Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I had encountered a problem, I desperately need its solution.
In my design, I have a gridview and an sql data source attached to it, along with that, I have an html table also.
html Table has 3 rows with each row having 3 columns. Each cell consists of textboxes i.e.,
First row has textbox1, textbox2, textbox3. In the same way,
Second row has textbox4, textbox5, textbox6 likewise the same in third row textbox7,8,9....
For gridview, i have a select ommand on each row.
For binding data from gridview to 1st row of html table, we write
SQL
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
TextBox3.Text = GridView1.SelectedRow.Cells[3].Text;

The above code works good but after that if I need to bind data to second row of the table, how should I do it?
Posted
Updated 31-Oct-12 23:12pm
v2

1 solution

C#
int rowcount = 0;
int.TryParse(ViewState["rowcount"] + "", out rowcount);

if (rowcount == 0)
{
    rowcount++;
    ViewState["rowcount"] = rowcount;
    TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
    TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
    TextBox3.Text = GridView1.SelectedRow.Cells[3].Text;
}
else if (rowcount == 1)
{
    rowcount++;
    ViewState["rowcount"] = rowcount;
    TextBox4.Text = GridView1.SelectedRow.Cells[1].Text;
    TextBox5.Text = GridView1.SelectedRow.Cells[2].Text;
    TextBox6.Text = GridView1.SelectedRow.Cells[3].Text;
}
else if (rowcount == 2)
{
    rowcount++;
    ViewState["rowcount"] = rowcount;
    TextBox7.Text = GridView1.SelectedRow.Cells[1].Text;
    TextBox8.Text = GridView1.SelectedRow.Cells[2].Text;
    TextBox9.Text = GridView1.SelectedRow.Cells[3].Text;
}
 
Share this answer
 
Comments
Bhargav.456 1-Nov-12 5:58am    
Thank You, For example, If we have two different tables say table1 and table2. Both tables have 1 row, 5 columns each(each column has five textboxes naming from textbox1 to textbox5 for table1 and textbox6 to textbox10 for table2).
I placed those tables one by one i.e, first table1 as 1 row then table2 as 2nd row.
At first instance, For binding data from gridview (on select command) to table1, we write TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[2].Text; etc. . then at next 2nd row i.e., we have table2. How can I insert data in that from gridview?
adriancs 1-Nov-12 7:50am    
Can you show the codes that you build table1, table2 and gridview?
Bhargav.456 1-Nov-12 8:11am    
TABLES:
<asp:Panel ID="Panel1" runat="server">
<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server">
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server">
</td>
</tr>
</table>

<asp:Panel ID="Panel2" runat="server">
<table>
<tr>
<td>
<asp:TextBox ID="TextBox6" runat="server">
</td>
<td>
<asp:TextBox ID="TextBox7" runat="server">
</td>
<td>
<asp:TextBox ID="TextBox8" runat="server">
</td>
</tr>
</table>


GRIDVIEW Code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged2">
<columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="Eup" HeaderText="Eup" SortExpression="Eup" />
<asp:BoundField DataField="Mrp" HeaderText="Mrp" SortExpression="Mrp" />


<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:sruthiConnectionString %>"
SelectCommand="SELECT [Code], [Description], [UnitPrice], [Eup], [Mrp] FROM [sample_tbl]">

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