I keep getting index out of range errors when updating values in a datatable. Please see code below and offer any suggestions on a solution. In general, if the updates are slow, the datagridview does not throw exceptions. When the updates are more frequent, exceptions get thrown.
I've tried several different methods of updating the datagridview, as can be seen in the code. Any suggestions would be greatly appreciated.
I've tried several different methods of updating the datagridview, as can be seen in the code. Any suggestions would be greatly appreciated.
Code:
public Form1()
{
InitializeComponent();
}
DataTable myDT;
private void button1_Click(object sender, EventArgs e)
{
procDGVTEST();
}
private void procDGVTEST()
{
myDT = new DataTable();
string strConn = "Server ...Integrated Security=True;";
string strCMD = "Use TestResults select * from test";
SqlConnection mycn = new SqlConnection(strConn);
SqlDataAdapter myda = new SqlDataAdapter(strCMD,mycn);
myda.Fill(myDT);
myDT.TableName = "myDT";
//set datasource to datatable directly doesn't Work
//dataGridView1.DataSource = myDT;
//use a binding source - doesn't Work!
//BindingSource bSource = new BindingSource();
//bSource.DataSource = myDT;
//dataGridView1.DataSource = bSource;
//use dataview doesn't work
//DataView view = new DataView(myDT);
//dataGridView1.DataSource = view;
DataSet myDS = new DataSet();
myDS.Tables.Add(myDT);
dataGridView1.DataSource = myDS;
dataGridView1.DataMember = "myDT";
dataGridView1.AutoGenerateColumns = false;
}
private void procCounter()
{
for (double i = 0; i < 1200000000000; i++)
{
int updateN = 5000;
if ((i % updateN) == 0)
{
//try
//{
//myDT.Rows[0][2] = i;
//dataGridView1.Rows[0].Cells[2].Value = i;
this.dataGridView1[2, 0].Value = i;
//}
//catch (Exception e)
//{
// MessageBox.Show(e.ToString());
//}
}
}
}
private delegate void myDel();
private void button2_Click(object sender, EventArgs e)
{
//myDel del = new myDel(procCounter);
//del.Invoke();
Thread myThread = new Thread(new ThreadStart (procCounter));
myThread.Start();
}
Comment