C# 异常处理错误4

C# 异常处理错误4,c#,asp.net,gridview,C#,Asp.net,Gridview,中发生“System.InvalidOperationException”类型的异常 System.Web.dll,但未在用户代码中处理 其他信息: 在检索选定的数据键之前,必须在GridView“GridView1”上指定数据键。使用DataKeyNames属性指定数据键 GridView1\u SelectedIndexChanged的代码段包含罪魁祸首: display += Convert.ToString(GridView1.SelectedValue); 你得到的异常解释了你需要做

中发生“System.InvalidOperationException”类型的异常 System.Web.dll,但未在用户代码中处理

其他信息:

在检索选定的数据键之前,必须在GridView“GridView1”上指定数据键。使用DataKeyNames属性指定数据键

GridView1\u SelectedIndexChanged的代码段包含罪魁祸首:

display += Convert.ToString(GridView1.SelectedValue);

你得到的异常解释了你需要做什么

在检索选定的数据键之前,必须在GridView“GridView1”上指定数据键。使用DataKeyNames属性指定数据键

在GridView中,您必须定义哪个字段(属性)应该作为键。这通常是数据中的标识符(例如ID)属性。您可以使用
GridView
上的
DataKeyNames
属性执行以下操作:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string display = "Combo="; 
    int s; 
    total_cost = 0; 
    s = GridView1.SelectedIndex; 
    switch (s) 
    { 
        case 0: 
            bool isInt = int.TryParse(lbl_total_price.Text, out total_cost ); 
            display += Convert.ToString(GridView1.SelectedValue); 
            lbl_combo_cost.Text = "100"; 
            total_cost += 100; 
            break;
    }
}


在这里,
MyId
应该替换为作为键的属性的名称。

您得到的异常解释了您需要做什么

在检索选定的数据键之前,必须在GridView“GridView1”上指定数据键。使用DataKeyNames属性指定数据键

在GridView中,您必须定义哪个字段(属性)应该作为键。这通常是数据中的标识符(例如ID)属性。您可以使用
GridView
上的
DataKeyNames
属性执行以下操作:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string display = "Combo="; 
    int s; 
    total_cost = 0; 
    s = GridView1.SelectedIndex; 
    switch (s) 
    { 
        case 0: 
            bool isInt = int.TryParse(lbl_total_price.Text, out total_cost ); 
            display += Convert.ToString(GridView1.SelectedValue); 
            lbl_combo_cost.Text = "100"; 
            total_cost += 100; 
            break;
    }
}


在这里,
MyId
应该替换为作为键的属性的名称。

您可以分享一个代码示例吗。什么是“显示”?受保护的无效GridView1\u SelectedIndexChanged(对象发送方,事件参数e){string display=“Combo=“;int s;总成本=0;s=GridView1.SelectedIndex;开关{case 0:bool isInt=int.TryParse(lbl\u total\u price.Text,out total\u cost);display+=Convert.ToString(GridView1.SelectedValue);lbl\u combo\u cost.Text=“100”;total_cost+=100;break;请编辑并更新您的帖子。上面显示的内容是我代码的一部分,当我运行时,我发现了错误,但我不明白其中的错误。请发布您的GridView1的代码。您可以共享代码示例吗?什么是“显示”?受保护的无效GridView1\u选择的索引已更改(object sender,EventArgs e){string display=“Combo=”;int s;total_cost=0;s=GridView1.SelectedIndex;开关{case 0:bool isInt=int.TryParse(lbl_total_price.Text,out total_cost);display+=Convert.ToString(GridView1.SelectedValue);lbl\u组合成本。Text=“100”;total_cost+=100;break;请编辑并更新您的帖子。上面显示的是我的代码的一部分,当我运行时,我发现了错误,但我不明白其中的错误。请发布您的GridView1的代码。非常感谢您…该错误已经消失…需要做更多的工作…希望您能进一步帮助我非常感谢你…那个错误已经过去了…还有更多的工作要做…希望你能进一步帮助我…谢谢