索引超出范围... [英] Index was out of range...
问题描述
大家好,
我有两个数据网格,一种形式,我用来自同一表但过滤器不同的sql表数据填充它们.然后,我从每个选定的数据网格行中获取一些值,并填充一些文本框.我使用单元格内容单击事件来捕获选择了哪个数据网格.当我在一个网格中选择一行并在第二个网格中选择一个列时,问题就会出现.有帮助吗?
这是我用来获取取决于rowindex的值的代码
Hello all,
I have two datagrids,in one form and i fill them with sql table data,from the same table,but different filter.Then i get some values from each selected row of datagrid,and fill some textboxes. I use the cell content click event to catch which datagrid is selected.The problem shows up when i select a row in one grid and a colum in second grid.The clear selection doesnt do the job. Any help??
Here is the code that i use to get the values depending on rowindex
private void dataGridEksp_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Global.MakeReadOnly(this.tabPage5);
i = dataGridEksp.SelectedRows[0].Index;
txt_rrugaEks.Text = dataGridEksp.Rows[i].Cells[0].Value.ToString();
cmb_grupi.Text = dataGridEksp.Rows[i].Cells[1].Value.ToString();
txt_pagShofer.Text = dataGridEksp.Rows[i].Cells[2].Value.ToString();
txt_pagPun.Text = dataGridEksp.Rows[i].Cells[3].Value.ToString();
txt_pagPunetorEkstra.Text = dataGridEksp.Rows[i].Cells[4].Value.ToString();
txt_pagShoferEkstra.Text = dataGridEksp.Rows[i].Cells[5].Value.ToString();
txt_ujeEks.Text = dataGridEksp.Rows[i].Cells[6].Value.ToString();
txt_emulEks.Text = dataGridEksp.Rows[i].Cells[7].Value.ToString();
txt_nafteEks.Text = dataGridEksp.Rows[i].Cells[8].Value.ToString();
txt_rereEks.Text = dataGridEksp.Rows[i].Cells[9].Value.ToString();
idCmime = dataGridEksp.Rows[i].Cells[10].Value.ToString(); ;
button_modifikoCmim.Enabled = true;
dataGridBrendshem.ClearSelection();
button_fshiCmim.Enabled = true;
GetId_Tip(this, null);
}
推荐答案
此错误可能是由于dataGridEksp.SelectedRows为null ...
因此,以下内容可能会对您有所帮助,
而不是使用..
this error is coming may be because dataGridEksp.SelectedRows is coming null...
so following may help you,
instead of using ..
txt_rrugaEks.Text = dataGridEksp.Rows[i].Cells[0].Value.ToString();
使用以下...
use following...
txt_rrugaEks.Text = dataGridEksp.Rows[e.RowIndex].Cells[0].Value.ToString();
休息像上面一样继续
或者您可以使用...
for rest continue like above
OR u can use...
i = e.RowIndex;
i = dataGridEksp.SelectedRows [0] .Index;这里说索引超出范围.
这意味着根本没有选定的行.在访问行之前,请确保有要访问的行.
i = dataGridEksp.SelectedRows[0].Index; here its says the index is out of range.
This means there is no selected row at all. Make sure that there is/are selected rows to access before accessing them.
这篇关于索引超出范围...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!