Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi
I want to create Gridview for the new database ( The database dose not have data).

I want when click on the button it give me empty gridview,by this gridview can I insert the data to the database.

The problem in my code is,it should be insert some data in the database before using the gridview to insert

This is my asp page code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" ShowFooter="True">
            <Columns>
                <asp:BoundField DataField="RowNumber" HeaderText="RowNumber"
                    InsertVisible="False" ReadOnly="True" SortExpression="RowNumber" />
                   <asp:TemplateField HeaderText="Column1" SortExpression="Column1">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Column1") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtColumn1" runat="server" Height="22px" Width="145px"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                 <asp:TemplateField HeaderText="Column2" SortExpression="Column2">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Column2") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtColumn2" runat="server" Height="22px" Width="203px"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Column2") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>


                     <asp:TemplateField HeaderText="Column3" SortExpression="Column3" Visible="false">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Column3") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtColumn3" runat="server" Height="22px" Width="203px"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Column3") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>

        </asp:GridView>

         <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ourProjectConnectionString %>"
              InsertCommand="INSERT INTO [Table1] ([Column1], [Column2], [Column3]) VALUES (@Column1, @Column2, @Column3)"
            SelectCommand="SELECT * FROM [Table1]">

              <InsertParameters>
                <asp:Parameter Name="Column1" Type="String" />
                <asp:Parameter Name="Column2" Type="String" />
                <asp:Parameter Name="Column3" Type="String" />
            </InsertParameters>

            </asp:SqlDataSource>


and the page of c# code is :



protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox txtColumn1 = GridView1.FooterRow.FindControl("txtColumn1") as TextBox;
            TextBox txtColumn2 = GridView1.FooterRow.FindControl("txtColumn2") as TextBox;
            TextBox txtColumn3 = GridView1.FooterRow.FindControl("txtColumn3") as TextBox;
            SqlDataSource1.InsertParameters["Column1"].DefaultValue = txtColumn1.Text;
            SqlDataSource1.InsertParameters["Column2"].DefaultValue = txtColumn2.Text;
            SqlDataSource1.InsertParameters["Column3"].DefaultValue = txtColumn3.Text;
            SqlDataSource1.Insert();
            GridView1.DataBind();
        }


Any one can help me ?

Thanks in Advance.
Posted
Updated 19-May-11 18:56pm
v4
Comments
Karthik. A 19-May-11 17:04pm    
Added pre tags

Have a look at this Editable Gridview example.

Editable GridView in ASP.NET 2.0[^]

By having this as a referance you can shape your gridview the way you want.
 
Share this answer
 
If there is no data in the database and you're wanting the GridView to display controls to allow the client to insert data you'll need to modify the EmptyDataTemplate to include a button (could also be a LinkButton if you prefer) and a textbox (or another control like the dropdownlist depending on the situation) for each value you want to insert

<EmptyDataTemplate>
        <table>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Insert" CommandName="EmptyInsert" />
                </td>
                <td><asp:TextBox ID="TextBox1" runat="server" Text=''></asp:TextBox></td>
                <td><asp:TextBox ID="TextBox2" runat="server" Text=''></asp:TextBox></td>
                <td><asp:TextBox ID="TextBox3" runat="server" Text=''></asp:TextBox></td>
            </tr>
        </table>
    </EmptyDataTemplate>




Notice that I have the CommandName Property set on the Button control. You'll need to use the OnRowCommand event of the gridview and in the code behind it should look something like

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Check that the command name matches that from the button
            if (e.CommandName.CompareTo("EmptyInsert").Equals(0))
            {
                GridView gv = sender as GridView;
                Table tbl = gv.Controls[0] as Table;
                string input1 = (tbl.Rows[0].FindControl("TextBox1") as TextBox).Text;
                string input2 = (tbl.Rows[0].FindControl("TextBox2") as TextBox).Text;
                string input3 = (tbl.Rows[0].FindControl("TextBox3") as TextBox).Text;

                // Clear the initial values
                SqlDataSource1.InsertParameters.Clear();
                SqlDataSource1.InsertParameters.Add(new Parameter("column1", TypeCode.String, input1));
                SqlDataSource1.InsertParameters.Add(new Parameter("column2", TypeCode.Int32, input2));
                SqlDataSource1.InsertParameters.Add(new Parameter("column3", TypeCode.Boolean, input3));

                SqlDataSource1.Insert();
                gv.DataBind();
            }
        }

Here we check that the current command name is equal to the command name from the insert button control. Next we find the textbox controls in the gridview's childtable control and pull the Text value from each one. After that we clear the existing insert parameters and enter our new ones ( I used random types here as an example). Finally, we use the SqlDataSource to insert the data and rebind our gridview to show the updated database values.
 
Share this answer
 
v3

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