在grideview中选中一行怎样获得这一行的编号
解决方案 »
- 求高手帮忙解决:如何DataList的字段值??老提示System.EventArgs并不包含"Item"的定义,代码如下
- ashx一般处理程序处理客户端COOKIE的问题,在本地可以,挂服务器用IIS测试就获取不到值
- 如何去掉Treeview自动生成的css样式?
- 急!急!急!网络打印机无法连接!在线等!!!!!!!QQ120547173谢谢!!!!
- DataGrid分页的问题
- 象下面的方法,能不能做两个输出参数?
- 请想要c#程序的网友进来看看
- DataList或Reapter中的button可以添加脚本吗?
- 高手求js下拉菜单!
- 用CreateObject("Excel.Application","server")建立远程服务怎么不行?
- 使用GridView的疑问
- 求“在指定时间内段里的提醒”的代码,100分求解简单问题,在线等
必须要在GridViewCommandEventArgs这个事件的处理函数中才能使用
例如点击了当前行是个LinkButton
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
LinkButton lb = (LinkButton)sender;
DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
GridViewRow gvr = (GridViewRow)dcf.Parent;
GridView1.SelectedIndex = gvr.RowIndex;
}
<asp:ButtonField ButtonType="Button" CommandName="Xyz" Text="按钮" />protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "Xyz")
Response.Write(e.CommandArgument.ToString());
}
另外一种情况是你启用了选定内容。
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(GridView1.SelectedIndex);
}还有一种情况是TemplateField内的<asp:Button>通过CommandArgument='<%# Container.DisplayIndex %>'传递行索引。取得行索引后再通过GridView1.DataKeys[行索引].Value.ToString()来取得对应的主关键字段值。
如果使用模板列的话,index = null,你需要在RowCreated对他赋值:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
}}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Button btn = (Button)e.Row.FindControl("Button9");
if (btn != null)
{
btn.CommandArgument = e.Row.RowIndex.ToString();
}
}protected void Button9_Command1(object sender, CommandEventArgs e)
{
Response.Write(e.CommandArgument.ToString());
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
{
e.Row.Attributes.Add("id",(e.Row.RowIndex+1).ToString());//这里可不写 e.Row.Attributes.Add("onclick","alert('你当前选择的是第'+this.id+'行!')");
//或者
//e.Row.Attributes.Add("onclick","alert('您当前选择的是第"+(e.Row.RowIndex+1)+"行!')");
// 或者
CheckBox check = e.Row.FindControl("checkbox1") as CheckBox;
if (check != null)
{
check.Attributes.Add("onclick","alert('你当前选择的是第"+(e.Row.RowIndex+1)+"行')");
}
}
}
<asp:GridView ID="GridView1" Font-Size="9pt" runat="server" HorizontalAlign="Center" OnRowDataBound="GridView1_RowDataBound" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:TemplateField><ItemTemplate><asp:CheckBox ID="checkbox1" runat="server" /></ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server">
<Columns><asp:BoundField DataField="id" /></Columns>
</asp:GridView>
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
{
e.Row.Attributes.Add("onclick","alert('当前行的数据id是"+e.Row.Cells[0].Text+"')");
}
int index1;
string tempsid = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
index1 = GridView1.Rows[i].RowIndex; tempsid = GridView1.DataKeys[index1]["sid"].ToString();//sid为此行数据在数据库里的主键号
}
}tempsid就是你要的
CustomersGridView.DataKeys[index].Value.ToString();//index为当前行的索引值
你可以使用sqlhelper,本文没用。代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{//清清月儿http://blog.csdn.net/21aspnet
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 表 set 字段1='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}//绑定
public void bind()
{
string sqlstr = "select * from 表";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "表");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };//主键
GridView1.DataBind();
sqlcon.Close();
}
}前台主要代码:
... ...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" />
<asp:BoundField DataField="姓名" HeaderText="用户姓名" />
<asp:BoundField DataField="员工性别" HeaderText="性别" />
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>