i retrieved the column name from the data base to the data grid view.
design as follows at run time in data grid view ;
Date Session colname colname colname colname colname
7/1/12 1
7/1/12 2
7/1/12 3
7/1/12 4
7/1/12 5
in the run time in the column name faculty name is retrieved from the data base.that is working fine.
Here Total Column is 7.
Data base record as follows;
date session faculty name
7/1/12 1 G Sekar
7/1/12 2 R suresh
7/1/12 3 V Raju
7/1/12 4 m Murali
7/1/12 5 R Joshi
suppose when i add one record in the data base total record as follows;
date session faculty name
7/1/12 1 G Sekar
7/1/12 2 R suresh
7/1/12 3 V Raju
7/1/12 4 m Murali
7/1/12 5 R Joshi
8/1/12 1 R Ram
BUTTON CODE AS FOLLOWS;
con.Open();
SqlCommand cmd = new SqlCommand("select Faculty_Name from Tb_SCH_Faculty_Details Where Active <> 'D' ", con);
SqlDataReader dr = cmd.ExecuteReader();
int j = 2;
while (dr.Read())
{
datagridView.Columns[j].HeaderText = dr[0].ToString().Trim();
j++;
}
dr.Close();
con.Close();
that time when i click the BUTTON ERROR SHOWS AS FOLLOWS;
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name:
please help me.from the button code please correct it, i tried.
Loading
Jignesh TrivediPosted Jan 9, 2013, 11:11 PM
I think this error due to no of column exceed. so i thing in this case you must generate you grid column dynamically.
example
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Hi there";
int colIndex = grid.Columns.Add(col);
http://www.opendebug.com/article/359540
hope this will help you.
chellappan pandiarajanPosted Jan 9, 2013, 7:45 AM
Have you come across your OLD Post(http://www.c-sharpcorner.com/Forums/Thread/198506/column-to-be-retrieved-from-the-data-base-in-the-dgv.aspx)....?
Pieter TwentyeightPosted Jan 9, 2013, 7:42 AM
The id you set is probably alreay in use.
If you go to the db, insert manually a new one and see wat the id is. It is possible if your last id was 24 that the new one will be 28.
If you calculate a new one that is 25 it will rase an error like that...