Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
how to pass datas from gridvew that has item template(textbox) to a datatable.

XML
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False"
                    DataKeyNames="proj_ID" DataMember="ss" style="text-align: justify"
                    Width="607px" Height="316px" onrowdatabound="gv1_RowDataBound">
                    <Columns>
                        <asp:BoundField HeaderText="Sl.No" />
                        <asp:BoundField HeaderText="Scheme code" DataField="Scheme_code" />
                        <asp:BoundField DataField="Proj_title" HeaderText="___Name_of_Scheme___" />
                        <asp:TemplateField HeaderText="Target">
                            <ItemTemplate>
                                <asp:TextBox ID="txb_tar" runat="server" Height="50px" TextMode="MultiLine"
                                    Width="160px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Achievements">
                            <ItemTemplate>
                                <asp:TextBox ID="txb_atch" runat="server" Height="50px" TextMode="MultiLine"
                                    Width="160px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Remarks">
                            <ItemTemplate>
                                <asp:TextBox ID="txb_rem" runat="server" Height="50px" TextMode="MultiLine"
                                    Width="160px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

C#
DataTable dt = new DataTable();

        if (gv1.HeaderRow != null)
        {

            for (int i = 0; i < gv1.HeaderRow.Cells.Count; i++)
            {
                dt.Columns.Add(gv1.HeaderRow.Cells[i].Text);
            }
        }


        foreach (GridViewRow row in gv1.Rows)
        {
            DataRow dr;
            dr = dt.NewRow();

            for (int i = 0; i < row.Cells.Count; i++)
            {
                dr[i] = row.Cells[i].Text.ToString();
            }
            dt.Rows.Add(dr);

        }
        gv2.DataSource = dt;
        gv2.DataBind();

this code only copies the data in normal cells.
first three cells are normal cells
and the rest three are template cells with textbox in item template.
when i tried to bind this to datatable..its not working..
can any one please help me

thanks in advance...
Posted
Updated 7-Jun-12 9:21am
v3

You can read the textbox content by following code

C#
TextBox textbx = (TextBox)gv1.FindControl("txb_tar");

string content=textbx.Text;


read in each row
 
Share this answer
 
v2
Comments
Ragi Gopi 7-Jun-12 6:14am    
thanks buddy
Hi, You almost have it right.

Make sure you define all the columns before you fill any cells.

So

C#
for (int i = 0; i < gv1.HeaderRow.Cells.Count; i++)
{
  dt.Columns.Add(gv1.HeaderRow.Cells[i].Text);
}


is good

then also add the column names for the 3 textboxes

the section where you fill the cells is also good just add code where you fill the cells for the columns associated with the textboxes.
 
Share this answer
 
Comments
Ragi Gopi 7-Jun-12 6:14am    
thanks buddy...
GOT RESULT....
C#
protected void bt_as_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        if (gv1.HeaderRow != null)
        {

            for (int i = 0; i < gv1.HeaderRow.Cells.Count; i++)
            {
                dt.Columns.Add(gv1.HeaderRow.Cells[i].Text);
            }
        }


        foreach (GridViewRow row in gv1.Rows)
        {
            DataRow dr;
            dr = dt.NewRow();

            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (i == 3)
                {
                    TextBox target = (TextBox)row.FindControl("txb_tar");
                    string content = target.Text;
                    dr[i] = content;

                }
                else if (i == 4)
                {
                    TextBox Achievements1 = (TextBox)row.FindControl("txb_atch");
                    string content = Achievements1.Text;
                    dr[i] = content;
                }
                else if (i == 5)
                {
                    TextBox Remarks1 = (TextBox)row.FindControl("txb_rem");
                    string content = Remarks1.Text;
                    dr[i] = content;
                }
                else
                {
                    dr[i] = row.Cells[i].Text.ToString();
                }
            }
            dt.Rows.Add(dr);

        }
        gv2.DataSource = dt;
        gv2.DataBind();
       

    }
 
Share this answer
 
v2

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