【问题标题】:Read a record selected in DataGridView table and read through info of that record into fields that is not shown in the table读取在 DataGridView 表中选择的记录,并将该记录的信息读取到表中未显示的字段中
【发布时间】:2014-03-27 15:23:44
【问题描述】:

注意:下面包含的一些代码已经在我的其他一些问题中使用过,但与其他问题有关。我还编辑了大部分帖子以减少混乱!

我最初包含 MessageBox.Show() 以查看记录是否循环并正确显示到字段中,但后来将其删除。例如,如果我打开一个文件,MessageBox.Show() 将出现并显示第一条记录,再次选择 OK 按钮并显示第二条记录,依此类推。而不是这个,我希望每一行都充当 MessageBox.Show() 并正确呈现相关行 - 即记录编号为 4 并且记录 4 显示。我可以读取文件中 X 条记录的数量,并且我知道如何在 DataGridView 表中创建 X 条记录。 应该发生什么:

  • 用户打开文件
  • 数据摘要填充到 DataGridView 表中
  • 选中的每一行都会在字段中更详细地显示数据。

我有一个 DataGridView SelectionChanged 方法,它下面只获取我选择的行号。我考虑将数字传递给我的 ReadWeldRecs 方法:

private void Dgv_SelectionChanged(object sender, EventArgs e)
{
    int rec_num_to_read = Dgv.CurrentRow.Index + 1;
    ReadWeldRecs(rec_num_to_read);
}

目前,即使我只是尝试 FileStream.Seek 在我打开的文件中使用上面的行值查找一个位置,它也会崩溃。所以,我删除了 while 循环,数据返回空白(根本不读取)。

private void ReadWeldRecs(int row_to_read)
{
    byte[] Rec = new byte[1024];
    int length = Rec.Length;

    using (FileStream Fs = new FileStream(import.FileName, FileMode.Open, FileAccess.Read))
    using(BinaryReader Br = new BinaryReader(Fs))
    {
        while ((length = Br.Read(Rec, 0, 1024)) > 0) // IF I REMOVE THIS IT DOESN'T CRASH BUT IT DOESN'T READ ANY DATA AND COMES BACK BLANK
        {
            Fs.Seek(row_to_read * 1024, SeekOrigin.Begin); 

            // Rest of code to read file information...
        }
    }
}

编辑

我现在可以在选择一行时激活从文件中读取数据的方法,但是数据是错误的 - 有些是正确的:/

private void Dgv_SelectionChanged(object sender, EventArgs e)
{
    byte[] Rec = new byte[1024];
    long length = file_info.Length;
    int rec_num_to_read = Dgv.CurrentRow.Index + 1;

    using (FileStream Fs = new FileStream(import.FileName, FileMode.Open, FileAccess.Read))
    using (BinaryReader Br = new BinaryReader(Fs))
    {
        while ((length = Br.Read(Rec, 0, 1024)) > 0)
        {
            foreach (var rec in Rec)
            {
                Fs.Seek(rec_num_to_read * 1024, SeekOrigin.Begin); // Position file to record

                Label_Product1.Text = DecodeString(Rec, 3, 12);
            }
        }
    }
}

【问题讨论】:

  • 您的代码确实有些不对劲。我不确定我是否理解您的目标,但您应该注意两点:1.您使用sum 调用ReadWeldRecs(sum);,但随后您使用RecordNumber = 0; 覆盖RecordNumber 。 2. while 循环条件是while (Rec.Length > 0) 但你不改变Rec 的大小,所以它相当于while (1024 > 0)while(true) {}。您应该设置 while(Rec_Len > 0) 并删除 break 语句。从这里开始,也许它会帮助你解决你的问题。
  • 这是有道理的,我明白为什么它不能正常工作。我已经按照您所说的将其应用于我的应用程序,现在它已经读取并显示了一条具有正确行数的记录(也删除了 MessageBox):) 只需要努力将记录启动到每个 DataGridView 行。谢谢!
  • 我尝试了您提供的 while 循环,但它使我的应用程序崩溃:/ 我不明白为什么会这样。
  • 可以将Read这一行移动到循环中:while ((Rec_Len = Br.Read(Rec, 0, 1024)) > 0),确保Rec_Len被正确初始化为大于0的值,这样才能先进入循环。说到崩溃,什么样的崩溃?抛出了什么异常?
  • 好吧,我会试试的。 “检测到 ContextSwitchDeadlock”的描述:CLR 无法从 COM 上下文 0x1b72dba0 转换到 COM 上下文 0x1b72ddf0 60 秒......(真的很长)。我现在只是简单地研究了这个异常,并且重复的建议是使用 BackgroundWorker。将调查异常和解决方法。

标签: c# winforms visual-studio datagridview .net-3.5


【解决方案1】:

下面的代码确实有效,但速度很慢:/所以我目前正在尝试解决它以提高速度。

byte[] Rec = new byte[1024];
FileInfo file_info = new FileInfo(import.FileName);
long a = file_info.Length;
int rec_num_to_read = Dgv.CurrentRow.Index + 1;

using (FileStream Fs = new FileStream(import.FileName, FileMode.Open, FileAccess.Read))
using (BinaryReader Br = new BinaryReader(Fs))
{
    while ((a = Br.Read(Rec, 0, 1024)) > 0)
    {
        Fs.Seek(rec_num_to_read * 1024, SeekOrigin.Begin); // I moved this out of the foreach loop

        foreach (var rec in Rec)
        {
            Label_Product1.Text = DecodeString(Rec, 3, 12);
            // Some more info to decode
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    相关资源
    最近更新 更多