我收到此错误:DatagridviewComboboxCell值无效

时间:2015-02-20 14:00:44

标签: c# winforms

 private void StudentDetails_Load(object sender, EventArgs e)
        {
            string query = "Select * from tbl_Branch";
            SqlDataAdapter da = new SqlDataAdapter(query, sqlConn);
            DataTable table = new DataTable();
            table.Rows.Add();
            da.Fill(table);
            DataGridViewComboBoxColumn GC = new DataGridViewComboBoxColumn();
            GC = (DataGridViewComboBoxColumn)gvStudentData.Columns["Branch"];
            GC.DataSource = table;
            GC.ValueType = typeof(string);
            GC.ValueMember = "BranchID";
            GC.DisplayMember = "Branch";
            //gvStudentData.Rows.Add();

            DataTable table1 = new DataTable();
            string query1 = "Select * from tbl_StudentDetails";
            SqlDataAdapter da1 = new SqlDataAdapter(query1, sqlConn);
            da1.Fill(table1);
            int j = 0;

            for (int i = 0; i < table1.Rows.Count; i++)
            {
                gvStudentData.Rows.Add();
                gvStudentData.Rows[j].Cells["USN"].Value = table1.Rows[i]["USN"].ToString();
                gvStudentData.Rows[j].Cells["FName"].Value = table1.Rows[i]["FName"].ToString();
                gvStudentData.Rows[j].Cells["MName"].Value = table1.Rows[i]["MName"].ToString();
                gvStudentData.Rows[j].Cells["LName"].Value = table1.Rows[i]["LName"].ToString();
                gvStudentData.Rows[j].Cells["Branch"].Value = table1.Rows[i]["BranchID"].ToString();
                gvStudentData.Rows[j].Cells["Semester"].Value = table1.Rows[i]["Semester"].ToString();

                j = j + 1;
            }

        }

我想在这种情况下使用2个数据源。一个用于组合框列,另一个用于显示提取的数据。我在表单加载时收到此错误。

2 个答案:

答案 0 :(得分:0)

更改此行:

gvStudentData.Rows[j].Cells["Branch"].Value = table1.Rows[i]["BranchID"].ToString();

gvStudentData.Rows[j].Cells["BranchId"].Value = table1.Rows[i]["BranchID"].ToString();

答案 1 :(得分:0)

数据源分配没有任何问题。只是没有数据源分配给另一个datagridviewcolumn。在表单加载事件到达其终点之前没有错误。

感谢您努力解决这个问题。