sachi vasishta

sachi vasishta

  • NA
  • 252
  • 0

datagridview operation

Jun 19 2009 8:46 AM

Hi
In a form I have a datagridview. I will retrieve 3 fields value  from a database table and put those values inside the first three rows of the datagridview. But It is throwing the IndexOutOfRangeException. I have written the following code.
And also I have set the background color of dgv to BlueVoilet but it is not showing that color.
private
void Form11_Load(object sender, EventArgs e)
{
dataGridView1.Columns.Add(
"c1","Application");
dataGridView1.Columns[0].Width = 350;
 
dataGridView1.BackColor =
Color.BlueViolet;
OdbcConnection ocon = new OdbcConnection("Dsn=testsample");
ocon.Open();
OdbcCommand ocom = new OdbcCommand();
ocom.Connection = ocon;
ocom.CommandText =
string.Format("select appid,name,date from appointment where appid=7");
OdbcDataReader
dr = ocom.ExecuteReader();
if (dr.Read())
{
dataGridView1.Rows[0].Cells[0].Value = dr.GetValue(0).ToString();
dataGridView1.Rows[1].Cells[0].Value = dr.GetValue(1).ToString(); // Here I am getting error
dataGridView1.Rows[2].Cells[0].Value = dr.GetValue(2).ToString();
}
ocon.Close();
}

Answers (1)