7

I have the following code:

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;

When I examine grid, the DataSource attribute indicates that the number of rows is correct. However, the grid.Rows count is zero.

In contrast, if I create a DataGridView on a winform, and then assign its DataSource to a DataTable, the DataGridView.Rows will be added automatically.

What code am I missing here, in order to have the DataGridView.Rows count correct?

4
  • Question is what is the display? Correct? Commented Nov 22, 2013 at 21:00
  • If you do not add the columns and simply specify the table as a datasource, do the rows display? Commented Nov 22, 2013 at 21:01
  • AutoGenerateColumns property? Commented Nov 22, 2013 at 21:02
  • I would suggest removing the column addition and maybe try grid.Refresh() Commented Nov 22, 2013 at 22:47

3 Answers 3

6

By adding this DataGridView control to the form programmatically it works :) Anybody would tell us why?

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;
Sign up to request clarification or add additional context in comments.

Comments

0
DataTable table = new DataTable();

//DataTable is filled with values here...

DataGridView grid = new DataGridView();

grid.DataSource = table;

Or

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;

}

Try this Code

Comments

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);
            }           
 }

go to website detail: 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.