以编程方式从 DataTable 创建 DataGridview

问题描述 投票:0回答:4

我有以下代码:

DataTable table = new DataTable();

//DataTable is filled with values here...

DataGridView grid = new DataGridView();

foreach (DataColumn column in table.Columns)
{
    grid.Columns.Add(column.ColumnName, column.ColumnName);
}

grid.DataSource = table;

当我检查

grid
时,
DataSource
属性表明行数是正确的。然而,
grid.Rows
计数为零。

相反,如果我在winform上创建一个

DataGridView
,然后将其
DataSource
分配给
DataTable
DataGridView.Rows
将自动添加。

为了使

DataGridView.Rows
计数正确,我在这里缺少什么代码?

c# winforms datagridview
4个回答
6
投票

通过以编程方式将此

DataGridView
控件添加到表单中,它可以工作:) 有人能告诉我们为什么吗?

DataTable table = new DataTable();

//DataTable is filled with values here...

DataGridView grid = new DataGridView();

// assuming (this) is a reference to the current form
this.Controls.Add(grid);

grid.DataSource = table;

0
投票
DataTable table = new DataTable();

//DataTable is filled with values here...

DataGridView grid = new DataGridView();

grid.DataSource = table;

或者

foreach (DataColumn column in table.Columns)
{
    grid.Columns.Add(column.ColumnName, column.ColumnName);
}

grid.Rows.Add(table.Rows.count);
Int32 i=0;
foreach (DataRow rw in table.Rows)
{
grid.Rows[i].Cell[0].value = rw["col1"].ToString();
i+=1;

}

试试这个代码


0
投票
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
 {
           try
            {
                if (e.ColumnIndex == 5)
                {
                    string Task = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
                    if ( Task == "Delete")
                    {
                        if (MessageBox.Show("Bạn có chắc chắm muốn xóa không?", "Đang xóa...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            int rowIndex = e.RowIndex;
                            dataGridView1.Rows.RemoveAt(rowIndex);
                            dataset.Tables["tbl_students"].Rows[rowIndex].Delete();
                            sqlAdapter.Update(dataset, "tbl_students");
                        }
                    }
                    else if(Task == "Insert")
                    {
                        int row = dataGridView1.Rows.Count - 2;
                        DataRow dr = dataset.Tables["tbl_students"].NewRow();
                        dr["id"] = dataGridView1.Rows[row].Cells["id"].Value;
                        dr["name"] = dataGridView1.Rows[row].Cells["name"].Value;
                        dr["address"] = dataGridView1.Rows[row].Cells["address"].Value;
                        dr["phone"] = dataGridView1.Rows[row].Cells["phone"].Value;
                        dr["email"] = dataGridView1.Rows[row].Cells["email"].Value;
                        dataset.Tables["tbl_students"].Rows.Add(dr);
                        dataset.Tables["tbl_students"].Rows.RemoveAt(dataset.Tables["tbl_students"].Rows.Count - 1);
                        dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 2);
                        dataGridView1.Rows[e.RowIndex].Cells[5].Value = "Delete";
                        sqlAdapter.Update(dataset, "tbl_students");
                    }
                    else if (Task == "Update")
                    {
                        int r = e.RowIndex;
                        dataset.Tables["tbl_students"].Rows[r]["id"] = dataGridView1.Rows[r].Cells["id"].Value;
                        dataset.Tables["tbl_students"].Rows[r]["name"] = dataGridView1.Rows[r].Cells["name"].Value;
                        dataset.Tables["tbl_students"].Rows[r]["address"] = dataGridView1.Rows[r].Cells["address"].Value;
                        dataset.Tables["tbl_students"].Rows[r]["phone"] = dataGridView1.Rows[r].Cells["phone"].Value;
                        dataset.Tables["tbl_students"].Rows[r]["email"] = dataGridView1.Rows[r].Cells["email"].Value;
                        sqlAdapter.Update(dataset, "tbl_students");
                        dataGridView1.Rows[e.RowIndex].Cells[5].Value = "Delete";
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }           
 }

前往网站详情:http://laptrinhvb.net/bai-viet/chuyen-de-csharp/Lap-trinh-ung-dung-them---xoa---sua-truc-tiep-tren-DataGridview -(Crub-database-on-DataGridview-use-Cshap)/e95de75579022678.html


0
投票

我有同样的问题,谁能告诉我为什么我必须:

this.Controls.Add(网格);

在它可以工作之前?

© www.soinside.com 2019 - 2024. All rights reserved.