将Gridviews数据显示在表单文本框中 [英] Displaying Gridviews data onto the forms Text box

查看:75
本文介绍了将Gridviews数据显示在表单文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击编辑按钮后在表格文本框控件上显示Gridview数据。

我该怎么办。

编辑按钮位于网格视图项目中模板列。

请给我一个简单的解决方案。

I want to display a Gridview data onto forms Text Box controls after clicking an edit button.
what should i do.
Edit button is in a Grid views item template column.
please give me simple solution.

推荐答案

如果你想让用户点击gridview中的值在gridview行中的某些编辑按钮上,您可以使用editItemTemplate。您可以在使用文本框编辑可编辑网格视图的代码项目中引用一篇简单的文章来执行此操作,CheckBox,单选按钮和DropDown列表 [ ^ ]



问候

Pawan
If you want to let the user edit the values in the gridview once they click on some "Edit" button in the gridview row then you can use the editItemTemplate. You might refer a simple article to do so on codeproject at Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List[^]

Regards
Pawan


将Gridviews数据显示到表格文本框 [ ^ ]





在此链接中你可以得到基本的想法。
Displaying Gridviews data onto the forms Text box[^]


in this link you can get basic idea.


试试这个...



Try this...

<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"

             AutoGenerateEditButton="True" onrowcancelingedit="GridView2_RowCancelingEdit" onrowediting="GridView2_RowEditing"

             >
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtNameGrid" runat="server" Text ='<%# Bind("Name") %> ' ></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %> '></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Age">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtAgeGrid" runat="server" Text = '<%# Bind("Age") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %> '></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
     </div>
     <div>
     <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
     <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
     </div>
    </form>










public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int key { get; set; }

}





public List< Person> obj = new List< Person>();

protected void Page_Load(object sender,EventArgs e)

{

obj = new List<人> {(new Person {Name =ajai,Age = 50,key = 10})};

obj.Add(new Person {Name =Manoj,Age = 100,key = 20});

obj.Add(new Person {Name =Sanjay,Age = 150,key = 30});



GridView2.DataSource = obj;

GridView2.DataBind();





}

protected void GridView2_RowEditing(object sender,GridViewEditEventArgs e)

{

GridView2.EditIndex = e.NewEditIndex;

GridView2.DataSource = obj;

GridView2.DataBind();

TextBox editName =(TextBox)GridView2.Rows [e.NewEditIndex] .Cells [0] .FindControl(txtNameGrid);

TextBox editAge =(TextBox)GridView2.Rows [e .NewEditIndex] .Cells [1] .FindControl(txtAgeGrid);

txtName.Text = editName.Text;

txtAge.Text = editAge.Text;

}

protected void GridView2_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)

{

GridView2.EditIndex = -1;

GridView2.DataSource = obj;

GridView2.DataBind();



}



public List<Person> obj = new List<Person>();
protected void Page_Load(object sender, EventArgs e)
{
obj = new List<Person>{(new Person{ Name = "ajai", Age = 50 ,key =10})};
obj.Add(new Person { Name = "Manoj", Age = 100, key = 20 });
obj.Add(new Person { Name = "Sanjay", Age = 150, key = 30 });

GridView2.DataSource = obj;
GridView2.DataBind();


}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
GridView2.DataSource = obj;
GridView2.DataBind();
TextBox editName = (TextBox)GridView2.Rows[e.NewEditIndex].Cells[0].FindControl("txtNameGrid");
TextBox editAge = (TextBox)GridView2.Rows[e.NewEditIndex].Cells[1].FindControl("txtAgeGrid");
txtName.Text = editName.Text;
txtAge.Text = editAge.Text;
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex = -1;
GridView2.DataSource = obj;
GridView2.DataBind();

}


这篇关于将Gridviews数据显示在表单文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆