首页 新闻 会员 周边

C# datagridview数据源是强类型,点击数据区域的时候提示 System.IndexOutOfRangeException:“索引 -1 没有值

0
悬赏园豆:10 [待解决问题]

以下是代码, 数据源 listzzfson 是强类型,点击datagridview数据区域的时候,老是提示
System.IndexOutOfRangeException:“索引 -1 没有值 报错, 是不是datagridview 不太兼容强类型呢? 以下是代码 :

private void butselectCOPTD_Click(object sender, EventArgs e)
{
//业务员一定要选择,如果不选择,不能添加销售订单
if (txtYWcode.SelectedValue == null)
{
MessageBox.Show("业务员一定要填写,不填写,不能添加销售订单");
return;
}
string mv001 = txtYWcode.SelectedValue.ToString();
SELECTCOPTD sd=new SELECTCOPTD(mv001);
sd.ShowInTaskbar = false;
sd.ShowDialog();
if (sd.DialogResult == DialogResult.OK && sd.dataGridView1.CurrentRow != null)
{
string TD001 = sd.dataGridView1.CurrentRow.Cells["TC001"].FormattedValue.ToString();
string TD002 = sd.dataGridView1.CurrentRow.Cells["TC002"].FormattedValue.ToString();
string custom = sd.dataGridView1.CurrentRow.Cells["MA002"].FormattedValue.ToString();
//获取订单的强类型
Maticsoft.BLL.COPTD coptdbll=new Maticsoft.BLL.COPTD();
//过滤条件
string strwhere = $"TD001='{TD001}' AND TD002='{TD002}'";
List<Maticsoft.Model.COPTD> listcoptd = coptdbll.GetModelList(strwhere);
foreach (Maticsoft.Model.COPTD imodel in listcoptd)
{
Maticsoft.Model.ZZFson modelson = new Maticsoft.Model.ZZFson();
modelson.TD001= imodel.TD001 ;
modelson.TD002= imodel.TD002 ;
modelson.TD003= imodel.TD003 ;
listzzfson.Add(modelson);
}
dataGridView1.DataSource = null;
dataGridView1.DataSource = listzzfson;

        }
        sd.Close();//显示关闭窗体
    }
郑英灿的主页 郑英灿 | 初学一级 | 园豆:156
提问于:2023-10-25 20:33

大概看了下你的代码,在弹出窗体应该设置一个可以返回的对象,而不是将弹出窗体的GirdView设置为Public,而且当窗体关闭后,关闭窗体的gridview 的当前行是否还能获取的值,是有待考究的!

dotnettalk 8个月前
< >
分享
所有回答(2)
0

加个判断:

if(索引大于零)
{
    //业务代码
}
重生之我是大彩笔 | 园豆:202 (菜鸟二级) | 2023-10-26 11:21

不是这个问题,是load下面不能 加绑定

支持(1) 反对(0) 郑英灿 | 园豆:156 (初学一级) | 2023-10-30 08:23
-1

在你的代码中,你将dataGridView1的数据源设置为listzzfson,而当你点击dataGridView1的数据区域时,可能会触发事件,试图获取选中行的某些值。如果没有选中行,就会出现System.IndexOutOfRangeException错误。这个错误表示你尝试获取一个无效的行索引。

为了解决这个问题,你可以在尝试获取选中行之前,确保有选中的行。你可以使用CurrentRow属性来检查是否有选中行,然后再获取值。在你的代码中,可以像这样修改:

csharp
Copy code
if (sd.DialogResult == DialogResult.OK && sd.dataGridView1.CurrentRow != null)
{
string TD001 = sd.dataGridView1.CurrentRow.Cells["TC001"].FormattedValue.ToString();
string TD002 = sd.dataGridView1.CurrentRow.Cells["TC002"].FormattedValue.ToString();
string custom = sd.dataGridView1.CurrentRow.Cells["MA002"].FormattedValue.ToString();

// 获取订单的强类型
Maticsoft.BLL.COPTD coptdbll = new Maticsoft.BLL.COPTD();

// 过滤条件
string strwhere = $"TD001='{TD001}' AND TD002='{TD002}'";

List<Maticsoft.Model.COPTD> listcoptd = coptdbll.GetModelList(strwhere);

foreach (Maticsoft.Model.COPTD imodel in listcoptd)
{
    Maticsoft.Model.ZZFson modelson = new Maticsoft.Model.ZZFson();
    modelson.TD001 = imodel.TD001;
    modelson.TD002 = imodel.TD002;
    modelson.TD003 = imodel.TD003;
    listzzfson.Add(modelson);
}

dataGridView1.DataSource = null;
dataGridView1.DataSource = listzzfson;

}
这个修改将检查sd.dataGridView1.CurrentRow是否为null,以确保有选中的行,然后再尝试获取单元格的值。这应该能够解决System.IndexOutOfRangeException错误。

Technologyforgood | 园豆:6790 (大侠五级) | 2023-10-28 21:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册