日期:2014-05-18  浏览次数:20955 次

获取DataGridGiew中绑定按钮所在行?
DataGridGiew中每行都绑定了动态添加的按钮控件,我想单击按钮能获取到行号,
按钮单击时,这行并没有被触发,我怎样能获取到行号?
或者单击按钮时触发这行也行。我主要是想获取到这行数据。
怎样能够实现?注:按钮是绑定添加到DataGridGiew中的。

还有DataGridGiew中DataGridViewComboBoxColumn这列,我可以只更改这个控件的宽度,而不改变列的宽度吗?

------解决方案--------------------
页面代码关键代码:
<asp:GridView ID="grvzcan" DataKeyNames="zcid" HeaderStyle-CssClass="grvheader" runat="server" BorderColor="#3387D7" BorderWidth="1px"
Width="976px" AutoGenerateColumns="False" CssClass="grvdanju" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="grvzcan_RowDataBound">
<Columns>
<asp:BoundField DataField="zcid" HeaderText="单据编号" />
<asp:BoundField DataField="did" HeaderText="主单据编号" />
<asp:TemplateField HeaderText="单据名称">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("zcan") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("zcname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="znumber" HeaderText="数量" />
<asp:BoundField DataField="zsums" HeaderText="金额" />
<asp:BoundField DataField="zbeizhu" HeaderText="资产描述" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="修改">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="libtnxuanzhe" runat="server" Text="修改" CommandName='<%#Eval("zcid")%>' OnCommand="libtnxuanzhe_Command">修改</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="grvheader" />
</asp:GridView>


后台代码:

 protected void grvzcan_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//加光棒效果
e.Row.Attributes.Add("onmouseover", "current=this.style.backgroundColor;this.style.backgroundColor='#CCFFFF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=current");

//添加点击行事件
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(e.Row.Cells[0].FindControl("libtnxuanzhe"), ""));
e.Row.Attributes.CssStyle.Add("cursor", "hand");

}
}

//点击行选择按钮或行时将对应的信息绑定到文本框中显示。
protected void libtnxuanzhe_Command(object sender, CommandEventArgs e)
{
int zcid = int.Parse(e.CommandName);
Session["zcidse"] = zcid;
zchan zcan = zchanmaager.getzchanByzcid(zcid);
this.txtzcdaim.Text=zcan.Cpin.Cpid.ToString();
this.txtcname.Text=zcan.Cpin.Cname;
this.txtcdanwei.Text = zcan.Cpin.Cdanwei;
this.txtnumber.Text = zcan.Znumber.ToString();
this.txtcdanjia.Text = zcan.Cpin.Cdanjia.ToString();
this.txtnumber.Text = zcan.Znumber.ToString();
this.txtcnumber.Text = zcan.Cpin.Cnumber.ToString();
this.txtzsums.Text = zcan.Zsums.ToString();
this.txtzbeizhu.Text = zcan.Zbeizhu;

}
 


------解决方案--------------------
C# code