Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

IndexOutOfRangeException on DataGridView

$
0
0

Hello,

 

I've come across a problem that's been driving me nuts for the past couple days.  I'll explain first what this part of the form is being used for.  Have two datagridviews that is giving me status information about another piece of software I'm monitoring.  The first dgv is used just to report an Online / Offline status.  The Second is used to give me vital stats about all the machines running that software (IPAddress, CPU Usage, Memory available, Memory used, Total Memory,....).  I have a class that polls for that status every 5 seconds.  Once it's finished polling, I call my form and pass a dataset containing two datatables.  At that point I'm setting the datasource of my two dgv's to the datatables within that dataset.  The problem I'm running into is a completly random error.  I can't find a single common denominator that could explain why this is happening.   The error message is below, followed by my code.  Has anyone else run into this problem?  I've some some similar things, but the suggestions so far have not helped.

 

System.IndexOutOfRangeException: Index 0 does not have a value.
   at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)

 

(sorry it's pretty ugly when I copy/paste code in here...it removed all my tabs..)

Code Snippet

publicvoid RefreshStatus(DataSet Status) {

try {

UpdateMachineStatus(Status.Tables["MachineStatus"]);

UpdateMainStatus(Status.Tables["MainStatus"]);

}

catch (Exception ex) {

Program.log.Add("Error updating datagridview status.", ex);

}

}

privatevoid UpdateMachineStatus(DataTable StatusData) {

if (this.dgvMachineStatus.InvokeRequired) {

SetDataTableCallBack d = newSetDataTableCallBack(UpdateMachineStatus);

this.Invoke(d, newobject[] { StatusData });

}

else {

try {

if (StatusData.Rows.Count > 0) {

this.dgvMachineStatus.DataSource = StatusData;

this.dgvMachineStatus.Refresh();

}

}

catch (Exception ex) {

Program.log.Add("Error updaing Machine Status", ex);

}

}

}

privatevoid UpdateMainStatus(DataTable StatusData) {

if (this.dgvMainStatus.InvokeRequired) {

SetDataTableCallBack d = newSetDataTableCallBack(UpdateMainStatus);

this.Invoke(d, newobject[] { StatusData });

}

else {

try {

if (StatusData.Rows.Count > 0) {

this.dgvMainStatus.DataSource = StatusData;

this.dgvMainStatus.Refresh();

foreach (DataGridViewRow dr in dgvMainStatus.Rows) {

if (dr.Cells.Count > 0) {

if (dr.Cells[0].Value.ToString().Contains("Off")) {

dgvMainStatus.Rows[dr.Index].Cells[0].Style.BackColor =

Color.Red;

dgvMainStatus.Rows[dr.Index].Cells[0].Style.SelectionBackColor =

Color.Red;

}

else {

dgvMainStatus.Rows[dr.Index].Cells[0].Style.BackColor =

Color.Green;

dgvMainStatus.Rows[dr.Index].Cells[0].Style.SelectionBackColor =

Color.Green;

}

}

}

}

}

catch (Exception ex) {

Program.log.Add("Error updating Main Status", ex);

}

}

}

 

 

privatevoid SetDataGridViewErrorHandler() {

dgvFactoryStatus.DataError +=

newDataGridViewDataErrorEventHandler(dgv_DataError);

dgvMachineStatus.DataError +=

newDataGridViewDataErrorEventHandler(dgv_DataError);

dgvMainStatus.DataError +=

newDataGridViewDataErrorEventHandler(dgv_DataError);

}

 

 

void dgv_DataError(object sender, DataGridViewDataErrorEventArgs e) {

Program.log.Add("Error updating Datagridview. Context:" + e.Context.ToString(), e.Exception);

ErrorCount = ErrorCount + 1;

lblErrors.Text =

"Form Errors: " + ErrorCount;

}

 

 

Also another thing I find is the errors seem to happen more frequently when I'm clicking around on my datagridview.  I'm not sure if that has anything to do with the problem or not.

 

Thanks,

Cory

 


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>