Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

How to insert data to already defined columns in datagridview

I have a datagridview with columns code and description.I am trying to fill it with the following code but its not filling it to the respective columns.
C#
 private void frmFind_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            string sql = "SELECT cuccd,cucna FROM incoun";
            using (SqlConnection connection = ConnectionManager.GetConnection())
            {
                SqlCommand command = new SqlCommand(sql, connection);
                SqlDataAdapter adp = new SqlDataAdapter(command);
                adp.Fill(ds,"INCOUN");
                dataGridView1.AutoGenerateColumns = false;

                dataGridView1.DataSource = ds.Tables["incoun"];
            }
}
Posted
Updated 16-Apr-11 6:18am
v2
Comments
Sandeep Mewara 16-Apr-11 12:18pm    
Use PRe tags to format code part.
Sandeep Mewara 16-Apr-11 12:19pm    
Any error happening?
Eager2Lern 16-Apr-11 12:38pm    
no errors its creating additional columns and filling data into it
Sandeep Mewara 16-Apr-11 12:57pm    
You mean dataGridView1.AutoGenerateColumns = false; not working?

1 solution

when AutoGenerateColumns is set to false, you must have specified the field names in each of the column

Go to 'Edit Columns', select a column and set the 'DataPropertyName' to the column name in your data table & then assign the datasource
 
Share this answer
 
Comments
Eager2Lern 16-Apr-11 13:25pm    
Your solution not working...
strogg 16-Apr-11 14:15pm    
Try setting the datasource to ds.Tables[0]. Also make sure it has some data in it. Make sure you have 2 pre-defined columns in the datagridview & their 'DataPropertyName' set to 'cuccd' and 'cucna'

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