Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
Posted
Updated 29-Mar-16 20:21pm
Comments
Nelek 23-May-13 6:48am    
MuhammadUSman1 23-May-13 7:10am    
You can enable row_edit on button click or according to your requirements. i think there is no need to add textbox or something.

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.

C#
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.
 
Share this answer
 
v2
Comments
Raju2049 23-May-13 7:41am    
Thanks! Mahesh,
It is working fine,but I am not able to save value to datatable after some changing.
Member 11819086 8-Oct-15 7:05am    
Dear Raju please send me where i have to set above GridView1_RowDataBound() in my code..???please help me

Member 10856300 25-Jun-14 8:53am    
how to get data from textBox ?
Hello Raju,

You have to create
HTML
ItemTemplate
for placing textbox on gridview cell.

For retrieving items from textbox you have to find control from gridview datarow.
e.g.
C#
foreach (GridViewRow row in gvShoppingCart.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                 int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);
            }
        }

Thanks,
Imdadhusen
 
Share this answer
 
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
}
}

}
 
Share this answer
 
Comments
Member 10856300 25-Jun-14 8:40am    
TextBox txt = (TextBox)GridView1.Rows[row].Cells[col].Controls[0]; not get data :|
Member 10856300 25-Jun-14 8:42am    
its error messages :

"Specified argument was out of the range of valid values.
Parameter name: index"
there is no need of adding textboxes in gridview when u can directly edit them afer adding textboxcolumn.

to retrieve the value from a cell of gridview

string myCellvalue=gridview.rows[gridview selected rowindex].cells["columnname"].value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900