Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating a datagridview dynamically from the database. I retrieved all my values from the database to the data table. but when I checked data table has 3 rows but datagridview has 0 rows.

What I have tried:

DataGridView gv1 = new DataGridView();
DataTable dtb111 = new DataTable();
// gv1.ColumnCount = 5;
// gv1.RowCount = 12;

DataColumn Dcolumn1 = new DataColumn("SNo", typeof(int));
Dcolumn1.AutoIncrement = true;
Dcolumn1.AutoIncrementSeed = 1;
Dcolumn1.AutoIncrementStep = 1;
dtb111.Columns.Add(Dcolumn1);
dtb111.Columns.Add("Description", typeof(string));
dtb111.Columns.Add("Quantity", typeof(string));
dtb111.Columns.Add("UnitPrice", typeof(string));
dtb111.Columns.Add("Total", typeof(string));

sqlDaa.Fill(dtb111);
gv1.AutoGenerateColumns = false;

gv1.DataSource = dtb111;

breakpint check result:
dtb111.rows.count = 3
gv1.rows.count = 0;
gv1.datasource.Rows.Count = 3


same result for second method. so main problem is gv1.rows.count


Second way i tired:

DataGridView gv1 = new DataGridView();
DataTable dtb111 = new DataTable();
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "";
col1.HeaderText = "S No.";
col1.Name = "S No.";
gv1.Columns.Add(col1);

DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
col2.DataPropertyName = "Description";
col2.HeaderText = "Description";
col2.Name = "Description";
gv1.Columns.Add(col2);


DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();
col3.DataPropertyName = "Quantity";
col3.HeaderText = "Quantity";
col3.Name = "Quantity";
gv1.Columns.Add(col3);

DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn();
col4.DataPropertyName = "UnitPrice";
col4.HeaderText = "UnitPrice";
col4.Name = "UnitPrice";
gv1.Columns.Add(col4);

DataGridViewTextBoxColumn col5 = new DataGridViewTextBoxColumn();
col5.DataPropertyName = "Total";
col5.HeaderText = "Total";
col5.Name = "Total";
gv1.Columns.Add(col5);
sqlDaa.Fill(dtb111);
gv1.AutoGenerateColumns = false;
gv1.DataSource = dtb111;
Posted
Updated 17-Mar-20 19:52pm

1 solution

The easy way is to use LINQ to SQL. You can learn this technique in just one or two hours. Here are the steps:

1. Create new LINQ to SQL in your C# Project.

2. Drag and Drop all Tables of in the Database. (You can see how to add tables to Datacontext)

3. Add Datasource and Finally

4. By opening new form drag and drop the bounded datagrideview to your form

5. Set its datasource the Datatacontext.Specific Table Name;

6. Run it; it will work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900