GridView 里面如何获取boundField点击的所在行的数据,在线
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "False " BackColor= "White "
BorderColor= "White " BorderStyle= "Ridge " BorderWidth= "2px " CellPadding= "3 " CellSpacing= "1 "
DataSourceID= "SqlDataSource1 " GridLines= "None " AllowPaging= "True " OnRowCommand= "GridView1_RowCommand " DataKeyNames= "帐号 ">
<FooterStyle BackColor= "#C6C3C6 " ForeColor= "Black " />
<Columns>
<asp:BoundField DataField= "帐号 " HeaderText= "帐号 " SortExpression= "帐号 " />
<asp:BoundField DataField= "姓名 " HeaderText= "姓名 " SortExpression= "姓名 " />
<asp:BoundField DataField= "身份证号 " HeaderText= "身份证号 " SortExpression= "身份证号 " />
<asp:BoundField DataField= "工号 " HeaderText= "工号 " SortExpression= "工号 " />
<asp:ButtonField CommandName= "EditRole " Text= "配置角色 " />
</Columns>
<RowStyle BackColor= "#DEDFDE " ForeColor= "Black " />
<SelectedRowStyle BackColor= "#9471DE " Font-Bold= "True " ForeColor= "White " />
<PagerStyle BackColor= "#C6C3C6 " ForeColor= "Black " HorizontalAlign= "Right " />
<HeaderStyle BackColor= "#4A3C8C " Font-Bold= "True " ForeColor= "#E7E7FF " />
</asp:GridView>
我在
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRole ")
{
string m_Login = " ";
m_Login = GridView1.DataKeys[GridView1.Rows..DataItemIndex].Value.ToString();
currencyLib.ShowMessage(this, " ",m_Login);
}
}
提示
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 34: {
行 35: string m_Login = " ";
行 36: m_Login = GridView1.DataKeys[GridView1.SelectedRow.DataItemIndex].Value.ToString();
行 37:
行 38: currencyLib.ShowMessage(this, " ",m_Login);
[解决办法]
string MessageID = gvMessage.DataKeys[e.Row.RowIndex][0].ToString();
[解决办法]
2005里面
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string pid = GridView1.SelectedDataKey.Value.ToString();
if (cart != null)
{
cart.DeleteItem(pid);
Profile.ShoppingCart.DeleteItem(pid);
Total.Text = String.Format( "合计:{0:c} ", cart.TotalCost);
//删除后的绑定,下面可以不要因为Page_Load事件绑定了
//ShoppingCart cart = (ShoppingCart)Session[ "MyShoppingCart "];
//GridView1.DataSource = cart.Orders;
GridView1.DataBind();
}
}
[解决办法]
this.GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value