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