如何动态地将文本框添加到GridView的每个单元格中,以及如何检索entred数据 [英] How can i add a textbox into a each cell of GridView dynamically and how can i retrieve entred data

查看:106
本文介绍了如何动态地将文本框添加到GridView的每个单元格中,以及如何检索entred数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们......

我正在从excel文件数据加载gridview。现在我想更改一些行数据。所以,我想在gridview的每个单元格中取一个文本框,从他们可以更改现有值并将其保存到数据库。请帮助我...

解决方案

下面是您可以通过其向GridView添加文本框的代码。这是一种动态方法,可用于您在设计时没有关于数据列的信息的情况。但是如果你在设计时有关于数据列的信息,请使用模板列方法。



  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for int i = 0 ; i < e.Row.Cells.Count; i ++)
{
TextBox txt = new TextBox();
txt.Text = e.Row.Cells [i] .Text;
e.Row.Cells [i] .Text = ;
e.Row.Cells [i] .Controls.Add(txt);
}
}
}





如果有效,请告诉我。


Hello Raju,



您必须创建

 ItemTemplate 

才能放置gridview单元格上的文本框。



要从文本框中检索项目,您必须从gridview数据行中找到控件。

例如

  foreach (GridViewRow行  gvShoppingCart.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
int quantity = int .Parse(((TextBox)row.Cells [ 1 ]。 FindControl( txtQuantity))。Text);
}
}



谢谢,

Imdadhusen


下面是示例代码为您提供基本的想法,您需要根据您的要求修改此代码



protected void Page_Load(object sender,EventArgs e)

{

string [] dataSource = {Cell 1,Cell 2,Cell 3};

GridView1.DataSource = dataSource;

GridView1.DataBind();

}



protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

{

if(e.Row.RowType == DataControlRowType.DataRow)

{

for(int i = 0; i < e.Row.Cells.Count; i ++)

{

TextBox txt = new TextBox();

txt.Text = e .Row.Cells [i] .Text;

e。 Row.Cells [i] .Text =;

e.Row.Cells [i] .Controls.Add(txt);



}

}

}





protected void btnSave_Click(object发件人,EventArgs e)

{

for(var row = 0;行< GridView1.Rows.Count;行++)

{

for(int col = 0; col< GridView1.Rows [row] .Cells.Count; col ++)

{

TextBox txt =(TextBox)GridView1.Rows [row] .Cells [col] .Controls [0];

string newVal = txt.Text;

//写逻辑来更新你的数据源

}

}



}

hi, friends...
I am loading gridview from excel file data. Now I want to change some row data. So, I want to take a textbox in each cell of gridview , from their i can change existing value and save it to database.Please help me...

解决方案

Below is the code through which you can add text box to GridView. This is dynamic approach and can be used in scenario where you don’t have information about data columns at design time. But if you have information about data column at design time go with Template column approach.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               for(int i =0; i < e.Row.Cells.Count; i++)
                {
                    TextBox txt = new TextBox();
                    txt.Text = e.Row.Cells[i].Text;
                    e.Row.Cells[i].Text = "";
                    e.Row.Cells[i].Controls.Add(txt);
                }
            }
        }



Please let me know if it works for you.


Hello Raju,

You have to create

ItemTemplate

for placing textbox on gridview cell.

For retrieving items from textbox you have to find control from gridview datarow.
e.g.

foreach (GridViewRow row in gvShoppingCart.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                 int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);
            }
        }


Thanks,
Imdadhusen


Below is the sample code to give you basic idea, you need modify this code as per your requirement

protected void Page_Load(object sender, EventArgs e)
{
string[] dataSource = { "Cell 1", "Cell 2", "Cell 3" };
GridView1.DataSource = dataSource;
GridView1.DataBind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for(int i =0; i < e.Row.Cells.Count; i++)
{
TextBox txt = new TextBox();
txt.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Text = "";
e.Row.Cells[i].Controls.Add(txt);

}
}
}


protected void btnSave_Click(object sender, EventArgs e)
{
for (var row = 0; row < GridView1.Rows.Count; row++)
{
for (int col = 0; col < GridView1.Rows[row].Cells.Count; col++)
{
TextBox txt = (TextBox)GridView1.Rows[row].Cells[col].Controls[0];
string newVal = txt.Text;
//write logic to update your datasource here
}
}

}


这篇关于如何动态地将文本框添加到GridView的每个单元格中,以及如何检索entred数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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