Answered by:
Pass automatic data from textbox to grid view

Question
-
User-1634117515 posted
Hi, I'm trying to get the data automatically passed to a gridview column when I type in a TextBox, this is what I'm doing in ASP.Net C # and this is what I've tried
protected void txtRepresentamos_TextChanged(object sender, EventArgs e) { GridView1.Rows[0].Cells[0].Text = txtRepresentamos.Text; }
Tuesday, November 14, 2017 3:00 PM
Answers
-
User2103319870 posted
markdirtyboy
Is an empty grid
ok then we need to take a different approach. Empty Grid are not rendered on page at runtime and that the reason we are getting this issue. To work around this issue what we can do is to add empty row to gridivew and then add values to that
Gridview Mark up like above
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Column1" HeaderText="Column1" /> <asp:BoundField DataField="Column2" HeaderText="Column2" /> <asp:TemplateField HeaderText="Column2Template"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:TextBox ID="txtRepresentamos" runat="server" OnTextChanged="txtRepresentamos_TextChanged" AutoPostBack="true"></asp:TextBox>
This time we add a empty row to gridview. This will make sure the gridview is rendered on page at runtime
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dtEmpty = new DataTable(); //Here ensure that you have added all the column available in your gridview dtEmpty.Columns.Add("Column1", typeof(string)); dtEmpty.Columns.Add("Column2", typeof(string)); DataRow datatRow = dtEmpty.NewRow(); //Inserting a new row,datatable .newrow creates a blank row dtEmpty.Rows.Add(datatRow);//adding row to the datatable GridView1.DataSource = dtEmpty; GridView1.DataBind(); } }
Now you can use below code
protected void txtRepresentamos_TextChanged(object sender, EventArgs e) { GridView1.Rows[0].Cells[0].Text = txtRepresentamos.Text; //Changing value of Template column in Gridview Label lbl = GridView1.Rows[0].Cells[0].FindControl("Label1") as Label; lbl.Text = txtRepresentamos.Text; }
However the problem with this approach an empty will be displayed on your page initially. If you dont want that then wrap gridview inside a panel and hide it. On TextChanged event you can make it visible
Updated Gridview markup
<asp:Panel ID="Panel1" runat="server" Style="display: none"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" > <Columns> <asp:BoundField DataField="Column1" HeaderText="Column1" /> <asp:BoundField DataField="Column2" HeaderText="Column2" /> <asp:TemplateField HeaderText="Column2Template"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </asp:Panel>
Updated TextChanged Event code
protected void txtRepresentamos_TextChanged(object sender, EventArgs e) { //Display the panel Panel1.Style["display"] = "block"; GridView1.Rows[0].Cells[0].Text = txtRepresentamos.Text; //Changing value of Template column in Gridview Label lbl = GridView1.Rows[0].Cells[0].FindControl("Label1") as Label; lbl.Text = txtRepresentamos.Text; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 14, 2017 4:40 PM
All replies
-
User724169276 posted
Can you please be more specific about the issue you are facing here ?
Tuesday, November 14, 2017 3:10 PM -
User2103319870 posted
GridView1.Rows[0].Cells[0].TextAbove code will work only if the column in Gridview is BoundColumn. if you have Template Column then we need to find the controls first and then assign text to the label control
Sample Code
protected void txtRepresentamos_TextChanged(object sender, EventArgs e) { GridView1.Rows[0].Cells[0].Text = txtRepresentamos.Text; //Changing value of Template column in Gridview Label lbl = GridView1.Rows[0].Cells[0].FindControl("Label1") as Label; lbl.Text = txtRepresentamos.Text; }
Complete Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Column1" HeaderText="Column1" /> <asp:BoundField DataField="Column2" HeaderText="Column2" /> <asp:TemplateField HeaderText="Column2Template"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:TextBox ID="txtRepresentamos" runat="server" OnTextChanged="txtRepresentamos_TextChanged" AutoPostBack="true"></asp:TextBox>
C#:
Populating Gridview with Sample Data
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt8 = new DataTable(); dt8.Columns.AddRange(new DataColumn[2] { new DataColumn("Column1"), new DataColumn("Column2") }); dt8.Rows.Add("Test1", "Address1"); dt8.Rows.Add("Test2", "Address1"); dt8.Rows.Add("Test3", "Address1"); GridView1.DataSource = dt8; GridView1.DataBind(); } }
Changing values in Gridview
protected void txtRepresentamos_TextChanged(object sender, EventArgs e) { GridView1.Rows[0].Cells[0].Text = txtRepresentamos.Text; //Changing value of Template column in Gridview Label lbl = GridView1.Rows[0].Cells[0].FindControl("Label1") as Label; lbl.Text = txtRepresentamos.Text; }
Tuesday, November 14, 2017 3:15 PM -
User2103319870 posted
Also make sure the You have set AutoPostBack property of textbox to true, this will ensure the TextChanged event is fired when textbox lost focus
<asp:TextBox ID="txtRepresentamos" runat="server" OnTextChanged="txtRepresentamos_TextChanged" AutoPostBack="true"></asp:TextBox>
Tuesday, November 14, 2017 3:18 PM -
User-1634117515 posted
I tried and i got this error
"Index was out of range. Must be non-negative and less then the size of the collection"
Parameter name : Index
Tuesday, November 14, 2017 4:03 PM -
User2103319870 posted
"Index was out of range. Must be non-negative and less then the size of the collection"Did you bind the gridview with data or is it an empty grid.
Tuesday, November 14, 2017 4:30 PM -
User-1634117515 posted
Is an empty grid
Tuesday, November 14, 2017 4:32 PM -
User2103319870 posted
markdirtyboy
Is an empty grid
ok then we need to take a different approach. Empty Grid are not rendered on page at runtime and that the reason we are getting this issue. To work around this issue what we can do is to add empty row to gridivew and then add values to that
Gridview Mark up like above
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Column1" HeaderText="Column1" /> <asp:BoundField DataField="Column2" HeaderText="Column2" /> <asp:TemplateField HeaderText="Column2Template"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:TextBox ID="txtRepresentamos" runat="server" OnTextChanged="txtRepresentamos_TextChanged" AutoPostBack="true"></asp:TextBox>
This time we add a empty row to gridview. This will make sure the gridview is rendered on page at runtime
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dtEmpty = new DataTable(); //Here ensure that you have added all the column available in your gridview dtEmpty.Columns.Add("Column1", typeof(string)); dtEmpty.Columns.Add("Column2", typeof(string)); DataRow datatRow = dtEmpty.NewRow(); //Inserting a new row,datatable .newrow creates a blank row dtEmpty.Rows.Add(datatRow);//adding row to the datatable GridView1.DataSource = dtEmpty; GridView1.DataBind(); } }
Now you can use below code
protected void txtRepresentamos_TextChanged(object sender, EventArgs e) { GridView1.Rows[0].Cells[0].Text = txtRepresentamos.Text; //Changing value of Template column in Gridview Label lbl = GridView1.Rows[0].Cells[0].FindControl("Label1") as Label; lbl.Text = txtRepresentamos.Text; }
However the problem with this approach an empty will be displayed on your page initially. If you dont want that then wrap gridview inside a panel and hide it. On TextChanged event you can make it visible
Updated Gridview markup
<asp:Panel ID="Panel1" runat="server" Style="display: none"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" > <Columns> <asp:BoundField DataField="Column1" HeaderText="Column1" /> <asp:BoundField DataField="Column2" HeaderText="Column2" /> <asp:TemplateField HeaderText="Column2Template"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </asp:Panel>
Updated TextChanged Event code
protected void txtRepresentamos_TextChanged(object sender, EventArgs e) { //Display the panel Panel1.Style["display"] = "block"; GridView1.Rows[0].Cells[0].Text = txtRepresentamos.Text; //Changing value of Template column in Gridview Label lbl = GridView1.Rows[0].Cells[0].FindControl("Label1") as Label; lbl.Text = txtRepresentamos.Text; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 14, 2017 4:40 PM