gridview的编辑模式编程 [英] gridview edit mode programatically
本文介绍了gridview的编辑模式编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个基本的GridView到我从背后code从SQL存储过程绑定的数据。
I have a basic gridview to which I am binding the data from the code behind from a SQL stored procedure.
<asp:GridView ID="gvCheckResults" runat="server" OnRowDataBound="gvCheckResults_RowDataBound" RowStyle-CssClass="gridViewRow" CssClass="gridView"
AlternatingRowStyle-CssClass="gridViewAlternatingRow" HeaderStyle-CssClass="gridViewHeader">
</asp:GridView>
数据绑定:
gvCheckResults.DataSource = dataContext.GetResults(Name, Address);
gvCheckResults.DataBind();
if (!IsPostBack)
{
//Add Edit column.
CommandField cField = new CommandField();
cField.EditText = "Edit";
cField.ShowEditButton = true;
gvCheckResults.Columns.Insert(0, cField);
}
我如何编程方式添加编辑按钮,每个结果行?
我知道该怎么做时,数据源对象是设置在aspx页面,但从来没有编程这样做。
How can I programmatically add Edit button to each result row? I know how to do it when the datasource object is setup in the aspx page, but never done it programmatically.
推荐答案
尝试像这样
<asp:TemplateField HeaderText="Edit>
<ItemTemplate>
<asp:LinkButton Id="lnkEdit" runat="server" CommandName="Edit" Text="Edit"/>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton Id="lnkEdit" runat="server" CommandName="Update" Text="Update"/>
<asp:LinkButton Id="LinkButton1" runat="server" CommandName="Cancel" Text="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
OnRowEditEvent()设置gridview的编辑索引e.newrowindex并再次绑定像这样。
OnRowEditEvent() set the gridview edit index to e.newrowindex and bind it again like this.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Write code TO UPDATE YOUR DATABESE THEN WRITE BELOW CODE IN LAST
// To Find Text of TextBox to get updated value....you get it in string like this.
//string strName = ((TextBox)grdview1.Rows[e.RowIndex].Cells[YourColumnIndexInWhichTexBoxAppear].Controls[0]).Text;
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
这篇关于gridview的编辑模式编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文