C# &引用;指数超出范围”;FormLoad时DataGridView SelectionChanged事件中引发异常
我在C# &引用;指数超出范围”;FormLoad时DataGridView SelectionChanged事件中引发异常,c#,datagridview,selectionchanged,C#,Datagridview,Selectionchanged,我在FormLoad中使用以下代码填充了名为DataSourceGrid的DataGridView String mquery = "SELECT ProductName,UnitPrice,CategoryName FROM ProductsTable INNER JOIN CategoryTable ON ProductsTable.Categoryid = CategoryTable.Categoryid"; using (SqlConnection con
FormLoad中使用以下代码填充了名为DataSourceGrid
的DataGridView
String mquery = "SELECT ProductName,UnitPrice,CategoryName FROM ProductsTable INNER JOIN CategoryTable ON ProductsTable.Categoryid = CategoryTable.Categoryid";
using (SqlConnection con = new SqlConnection(conString))
{
SqlDataAdapter sda = new SqlDataAdapter(mquery, con);
con.Open();
DataTable dt = new DataTable();
sda.Fill(dt);
DataSourceGrid.DataSource = dt;
}
我使用以下代码选择DataGridView
内部SelectionChanged
事件块中的行
int curRow = -1;
int curColumn = -1;
curRow = DataSourceGrid.CurrentRow.Index;
curColumn = DataSourceGrid.CurrentCell.ColumnIndex;
string firstCellValue = DataSourceGrid.SelectedRows[curRow].Cells[curColumn].Value.ToString();
if (DataSourceGrid.CurrentRow.Index >= -1)
{
using (SqlConnection con = new SqlConnection(conString))
{
String query = "SELECT ProductName,UnitPrice,CategoryName FROM ProductsTable INNER JOIN CategoryTable ON ProductsTable.Categoryid = CategoryTable.Categoryid WHERE ProductName='" + firstCellValue + "'";
con.Open();
SqlDataReader sdr = new SqlCommand(query, con).ExecuteReader();
sdr.Read();
ProductNameText.Text = sdr.GetValue(0).ToString();
UnitPriceText.Text = sdr.GetValue(1).ToString();
CategoryText.Text = sdr.GetValue(2).ToString();
}
当我单击一行时,数据将显示在相应的文本框中。我的问题是当FormLoad
抛出异常时,会说索引超出范围…
感谢您的支持。将您的代码更改为:
int curRow = DataSourceGrid.CurrentRow.Index;
int curColumn = DataSourceGrid.CurrentCell.ColumnIndex;
// First check for -1 and then do the rest.
if (curRow > -1 && curColumn > -1)
{
string firstCellValue = DataSourceGrid.SelectedRows[curRow].Cells[curColumn].Value.ToString();
数组的索引范围是从0
到Length-1
,因此如果索引是-1,那么它就是异常所说的OutOfIndex
如果希望在值为-1时访问第一个元素,则另一种解决方案如下
if (curRow >= -1 && curColumn >= -1)
{
curRow = curRow == -1 ? 0 : curRow;
curColumn = curColumn == -1 ? 0 : curColumn;
string firstCellValue = DataSourceGrid.SelectedRows[curRow].Cells[curColumn].Value.ToString();
将代码更改为:
int curRow = DataSourceGrid.CurrentRow.Index;
int curColumn = DataSourceGrid.CurrentCell.ColumnIndex;
// First check for -1 and then do the rest.
if (curRow > -1 && curColumn > -1)
{
string firstCellValue = DataSourceGrid.SelectedRows[curRow].Cells[curColumn].Value.ToString();
数组的索引范围是从0
到Length-1
,因此如果索引是-1,那么它就是异常所说的OutOfIndex
如果希望在值为-1时访问第一个元素,则另一种解决方案如下
if (curRow >= -1 && curColumn >= -1)
{
curRow = curRow == -1 ? 0 : curRow;
curColumn = curColumn == -1 ? 0 : curColumn;
string firstCellValue = DataSourceGrid.SelectedRows[curRow].Cells[curColumn].Value.ToString();
使用断点,然后检查这一行的变量DataSourceGrid.SelectedRows[curRow].Cells[curColumn]
我的spidey感官告诉我你会解决问题,很可能是-1
在哪一行?如果DataSourceGrid.CurrentRow.Index
为-1,你认为会发生什么?(提示:curRow
将为-1。)此外,也可能DataSourceGrid.CurrentRow
为null(这将导致另一个错误)。@Atk异常将在DataSourceGrid.SelectedRows[curRow].Cells[curColumn]
中抛出,当您尝试在string firstCellValue=DataSourceGrid.SelectedRows[curRow].Cells[curColumn].Value.ToString()中使用它时,您认为会发生什么情况代码>?请注意SelectedRows[curRow]
部分。使用断点,然后检查此行DataSourceGrid上的变量。SelectedRows[curRow]。Cells[curColumn]
my spidey senses告诉我你会解决问题,最有可能是-1
在哪一行?如果DataSourceGrid.CurrentRow.Index
为-1,您认为会发生什么?(提示:curRow
将为-1。)此外,也可能DataSourceGrid.CurrentRow
为null(这将导致另一个错误)。@Atk异常将在DataSourceGrid.SelectedRows[curRow].Cells[curColumn]
中抛出,当您尝试在string firstCellValue=DataSourceGrid.SelectedRows[curRow].Cells[curColumn].Value.ToString()中使用它时,您认为会发生什么情况代码>?注意SelectedRows[curRow]
部分。不起作用我用(curRow>=-1&&curColumn>=-1)
代替(curRow>-1&&curColumn>-1)
这样我就可以测试这两个条件。当值为-1时,必须删除equal,因为它将进入if close,这将抛出越界异常。检查更新的答案。如果我理解正确,它可能会解决你的问题。不工作兄弟。Idk我是否遗漏了问题中要提及的内容不工作我使用了(curRow>=-1&&curColumn>=-1)
而不是(curRow>-1&&curColumn>-1)
这样我就可以测试这两个条件。当值为-1时,必须删除equal,因为它将进入if close,这将抛出越界异常。检查更新的答案。如果我理解正确的话,可能会解决你的问题。不工作,兄弟。我想知道我是否遗漏了问题中要提及的内容