Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a one gridview.If no records in gridview means,i click the addnew button then display the textbox and dropdownlist in first row of gridview.It is possible...But, without using footer template.
Posted

I. Declare following globally:-
C#
DataTable mdtDescriptorText;
DataColumn objDcGetColoumns = new DataColumn();
DataRow objDrGetColoumns;


II. Create DataTable for Grid:
C#
private DataTable CreateDataTable()
{
    DataColumn col;
    mdtDescriptorText = new DataTable();
    col = new DataColumn("Column-1");
    mdtDescriptorText.Columns.Add(col);
    col = new DataColumn("Column-2");
    mdtDescriptorText.Columns.Add(col);
    col = new DataColumn("Column-3");
    mdtDescriptorText.Columns.Add(col);
    return (mdtDescriptorText);
}

III. Bind DataTable to GridView:
C#
private void AddNewRow(int intRowNumber)
{
    for (int i = 0; i < intRowNumber; i++)
    {
        objDrGetColoumns = mdtDescriptorText.NewRow();
        mdtDescriptorText.Rows.Add(objDrGetColoumns);
    }
    gridRow.DataSource = mdtDescriptorText.DefaultView;
    gridRow.DataBind();
}

IV. Now On Button click:
C#
protected void btnAddRow_Click(object sender, EventArgs e)
{
  CreateDataTable();
   foreach (GridViewRow gridItems in gridRow.Rows)
    {
        objDrGetColoumns = mdtDescriptorText.NewRow();
        TextBox txtBox1 = (TextBox)gridItems.FindControl("txtBox1");
        TextBox txtBox2 = (TextBox)gridItems.FindControl("txtBox2");
        TextBox txtBox3 = (TextBox)gridItems.FindControl("txtBox3");
        objDrGetColoumns["Column-1"] = txtBox1.Text;
        objDrGetColoumns["Column-2"] = txtBox2.Text;
        objDrGetColoumns["Column-3"] = txtBox3.Text;
        mdtDescriptorText.Rows.Add(objDrGetColoumns);
    }
  AddNewRow(1);
}

Here is your GridView:-
ASP.NET
<asp:GridView ID="gridRow" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Column-1">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBox1" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Column-2">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBox2" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Column-3">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBox3" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnAddRow" runat="server" Text="Add Row" OnClick="btnAddRow_Click" />


Good luck.
 
Share this answer
 
v3
u can try like that
protected void fnBindData()<pre lang="c#"></pre>
    {
        DataTable dt = new DataTable();
        dt = ViewState["data"] as DataTable;

        if (dt.Rows.Count &gt; 0)
        {
            gvTest.DataSource = dt;
            gvTest.DataBind();
        }
        else // when there is no data in your table 
        {
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            gvTest.DataSource = dt;
            gvTest.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