zhengyingcan 2023-10-25 20:39
浏览 2
已结题

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

以下是代码, 数据源 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();//显示关闭窗体
        }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月2日
    • 创建了问题 10月25日

    悬赏问题

    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害