1
我正在使用DataGridView来显示来自SQLite数据库的数据。一列是打开分配给该行的pdf的目录。代码工作,但我每次单击列标题时,它给我的错误:单击标题时DataGridView中出现“索引超出范围”异常
Index was out of range. Must be non-negative and less than the size of the collection.
其实,任何时候我请单击列文(只是“PDF”,或任何其他列的文字),它抛出那个错误。但是当我点击文本之外(在排序框中的任何位置)时,它会重新排列我的列,这是正确的。有任何想法吗?
该代码起作用,打开PDF,但我不希望用户不小心点击标题文本和程序崩溃。这里是datagridview打开pdf的代码。
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
string filename = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
if (e.ColumnIndex == 3 && File.Exists(filename))
{
Process.Start(filename);
}
}
谢谢!我知道我必须将rowIndex更改为-1,但我使用&&而不是||以类似的方式,您在我测试其他方式时编写代码。谢谢!像魅力一样工作! – Onlytito 2014-10-09 14:55:33
哎呀!一个小运营商造成了这么多问题。 ;) – 2014-10-09 14:57:44