【问题标题】:Not able to bind the grid view in code behind desktop application无法在桌面应用程序后面的代码中绑定网格视图
【发布时间】:2013-04-15 20:30:16
【问题描述】:

我有一个列出所有客户的网格视图。

我将它绑定在放置在 MDI 子级中的 Form 的加载时间中。

网格视图中的列是在设计时预定义的。

Form_Load() 事件的代码是:

try
{
       cn = db.createConnection();
       if (cn.State == System.Data.ConnectionState.Open)
       {
           cn.Close();
       }
       cn.Open();
       cmd = new OleDbCommand("Select BillNo,PartyName,City,State,FORMAT(BillDt,'dd-mm-yyyy') as BillDt from BillMaster", cn);
       da = new OleDbDataAdapter(cmd);
       ds = new DataSet();
       da.Fill(ds);
       cn.Close();

       dataGridView1.AutoGenerateColumns = false;
       dataGridView1.DataSource = ds.Tables[0];
       for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
       {
            dataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["BillNo"].ToString();
            dataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["PartyName"].ToString();
            dataGridView1.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["City"].ToString();
            dataGridView1.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["State"].ToString();
            dataGridView1.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["BillDt"].ToString();
        }

   }
   catch (Exception ex)
   {
        MessageBox.Show(ex.Message.ToString());
   }
   finally
   {
       cn.Close();
       da.Dispose();
       ds.Dispose();
       cmd.Dispose();
   }

}

我通过放置断点来检查程序的执行。数据在 DataSet 和即时窗口中与数据库完全相同,网格的特定单元格的值显示了确切的值,但问题是当加载表单时,网格仍然为空。并创建与从数据库中获取的行数相同的空白行数。

我应该怎么做才能解决这个错误。

请帮忙。

【问题讨论】:

  • 你能给我们看看你的 Designer.cs 文件吗?
  • 你的意思是设计表格吗?
  • this 是我的表单。但网格仍然是空的。预计会填充其中显示的数据。

标签: c# winforms data-binding datagridview desktop-application


【解决方案1】:

改变

dataGridView1.AutoGenerateColumns = false;

true。删除

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
   {
        dataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["BillNo"].ToString();
        dataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["PartyName"].ToString();
        dataGridView1.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["City"].ToString();
        dataGridView1.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["State"].ToString();
        dataGridView1.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["BillDt"].ToString();
    }

【讨论】:

  • 不确定我是否会将 AutoGenerateColumns 设置为 false,除非它们是预定义的
  • 是的,这些列是在设计时预定义的。
  • 那是因为它不存在 :)
【解决方案2】:

第一件事,也许我错过了什么,但是:

这是多余的,我会删除它,它可能是问题的一部分

 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
   {
        dataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["BillNo"].ToString();
        dataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["PartyName"].ToString();
        dataGridView1.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["City"].ToString();
        dataGridView1.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["State"].ToString();
        dataGridView1.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["BillDt"].ToString();
    }

这应该已经解决了

dataGridView1.DataSource = ds.Tables[0];

【讨论】:

  • 先生,我应该删除for 循环还是删除dataGridView1.DataSource = ds.Tables[0]; 语句? @Micah Armantrout
  • 如果为了好玩而将 autogen 列设置为 true 会发生什么
  • 当我将自动生成列设置为 true 时,它​​会提供空白网格视图作为输出。
  • 没有 MDI 应该没关系......所以无论 AutoGen 设置为 true 还是 false 都一样?
  • 是的,它对输出没有影响。
【解决方案3】:
try
{
   cn = db.createConnection();
   if (cn.State == System.Data.ConnectionState.Open)
   {
       cn.Close();
   }
   cn.Open();
   cmd = new OleDbCommand("Select BillNo,PartyName,City,State,FORMAT(BillDt,'dd-mm-yyyy') as BillDt from BillMaster", cn);
   da = new OleDbDataAdapter(cmd);
   ds = new DataSet();
   da.Fill(ds);
   cn.Close();

   dataGridView1.AutoGenerateColumns = true;
   dataGridView1.DataSource = ds.Tables[0];

   //Or you can use
   //dataGridView1.DataSource = ds.Tables[0].DefaultView;    


   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message.ToString());
   }
   finally
   {
   cn.Close();
   da.Dispose();
   ds.Dispose();
   cmd.Dispose();
  }

  }

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2015-09-22
    • 1970-01-01
    相关资源
    最近更新 更多