Click here to Skip to main content
16,018,442 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,
here is my code.
C#
private void btnShow_Click(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbl_stuInfo", cn);
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView1.Rows.Add(i + 1, dt.Rows[i][0], dt.Rows[i][1], dt.Rows[i][2], dt.Rows[i][3]);
            }
            dt.Clear();
        }

I want to show the data to dataGridView1, but there is an error in line
dataGridView1.Rows.Add(i + 1, dt.Rows[i][0], dt.Rows[i][1], dt.Rows[i][2], dt.Rows[i][3]);


I don't know why?

Please help me. Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 8-Mar-12 23:29pm    
What is datGridView? :-) And give us the full name. There are many similarly named controls in different libraries on .NET. Tag your application type and UI library.
--SA

1 solution

Hi Phanny,
You cannot explicitly add rows directly to the DataGridView.

You must instead add rows directy to your data source.Then refresh the DataGridView

Sample Code

C#
SqlDataAdapter da = new SqlDataAdapter("Select * from tbl_stuInfo", cn);
da.Fill(dt)
dataGridView1.DataSource = dt;




Thanks and Regards
Soumya

 
Share this answer
 
Comments
phanny 2011 9-Mar-12 2:43am    
thanks, it works. But It doesn't clear the old one. So when we click on button show, it always show the duplication data...How to do?

Thanks
Soumya Joseph 9-Mar-12 2:56am    
call the clear Function

dt.Clear();
phanny 2011 9-Mar-12 3:00am    
thanks so much......It works now.
Soumya Joseph 9-Mar-12 3:01am    
:)

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