获取DataGridGiew中绑定按钮所在行?

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

还有DataGridGiew中DataGridViewComboBoxColumn这列,我可以只更改这个控件的宽度,而不改变列的宽度吗?
...全文
123 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Josslin025 2012-07-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
DataGridGiew中每行都绑定了动态添加的按钮控件,我想单击按钮能获取到行号,
按钮单击时,这行并没有被触发,我怎样能获取到行号?
或者单击按钮时触发这行也行。我主要是想获取到这行数据。
怎样能够实现?注:按钮是绑定添加到DataGridGiew中的。

还有DataGridGiew中DataGridViewComboBoxColumn这列,我可以只更改这个控件的宽度,而不改变列的……
[/Quote]
此楼的信息根本没鸟用,一个bs里的,一个cs的,扯淡!
csdn_aspnet 2011-12-13
  • 打赏
  • 举报
回复
页面代码关键代码:
<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;

}


lvmeng0206 2011-12-13
  • 打赏
  • 举报
回复
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.dataGridView1.Columns["column名称"].Index)
{
DataGridViewButtonCell vCell = (DataGridViewButtonCell)dataGridView1.CurrentCell;
if (vCell.Tag == null)
{
//index
int i = e.ColumnIndex;
//修改数据
vCell.Value = "xxxx";
vCell.Tag = true;
return;
}
}
}


dataGridView1.Columns[0].MinimumWidth = xx;
dataGridView1.Rows[0].MinimumHeight = xx;
sdl2005lyx 2011-12-13
  • 打赏
  • 举报
回复

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
e.RowIndex //行号!
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧