kiran n
kiran n

Reputation: 67

Display Table data in Gridview

Here I am trying to display the data which is entered by user in table. I want to add this data in Gridview. Like in Table, user will enter data then this data has to save in Gridview and as well as it has to save in DataBase. Now I am trying to save this data in Gridview.

Here Table code for Entering the data. here user will enter the data.

    <asp:Table> //Table
    <asp:TableRow>
                            <asp:TableCell>
                                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                </asp:TableCell>
                             <asp:TableCell>
                                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                </asp:TableCell>
                             <asp:TableCell>
                                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                                </asp:TableCell>
                             <asp:TableCell>
                                 <asp:TextBox ID="FromDate" runat="server"></asp:TextBox>
                             </asp:TableCell>
                             <asp:TableCell>
                                 <asp:TextBox ID="ToDate" runat="server"></asp:TextBox>
                             </asp:TableCell>
                             <asp:TableCell>
                                <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
                                </asp:TableCell>
                        </asp:TableRow> 
                    </asp:Table>

Here is the button for adding the table data in Gridview.. When user clicks this button the data which entered by user in Table should display in gridview.

     <button id="AddMore_Button" class="btn btn-primary" onclick="Save_Info()">Add More</button>  //button

Here is the code for Gridview to show the information in table which entered by User. This gridview will display the data wich is entered in Table.

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                                    <Columns>
                                        <asp:BoundField HeaderText="Name" />
                                        <asp:BoundField HeaderText="Address" />
                                        <asp:BoundField HeaderText="Contact" />
                                        <asp:BoundField HeaderText="From Date" />
                                        <asp:BoundField HeaderText="To Date" />
                                        <asp:BoundField HeaderText="Website" />
                                    </Columns>

Here is the C# code for Display the data.. Here the data from table which entered by User will display in gridview.

    protected void Save_Info(object sender, EventArgs e)
            {
                DataTable dt = new DataTable(); //Datatable

                dt.Columns.Add(TextBox1.Text);
                dt.Columns.Add(TextBox2.Text);
                dt.Columns.Add(TextBox3.Text);
                dt.Columns.Add(FromDate.Text);
                dt.Columns.Add(ToDate.Text);
                dt.Columns.Add(TextBox6.Text);
                GridView1.DataSource = dt;  //Gridview
                GridView1.DataBind(); //binding data to Gridview
               }

When I click on "Add_More" button it is not saving the data in Gridview and not displaying the data. How can I achieve this using C#. Any wrong in this code..?? Please tell me.

Upvotes: 1

Views: 11151

Answers (2)

Ravi Kanth
Ravi Kanth

Reputation: 1210

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
   <Columns>
      <asp:BoundField HeaderText="Name" DataField="Name" />
      <asp:BoundField HeaderText="Address" DataField="Address"/>
      <asp:BoundField HeaderText="Contact" DataField="Contact"/>
      <asp:BoundField HeaderText="From Date" DataField="FromDate"/>
      <asp:BoundField HeaderText="To Date" DataField="ToDate"/>
      <asp:BoundField HeaderText="Website" DataField="Website"/>
   </Columns>

in C# code side you can do like this

DataTable dt = new DataTable();
protected void Save_Info(object sender, EventArgs e)
  {
    DataRow dr1 = dt.NewRow();

    dr1["Name"] =TextBox1.Text;
    dr1["Address"] =TextBox2.Text;
    dr1["Contact"] =TextBox3.Text;
    dr1["FromDate"] =FromDate.Text;
    dr1["ToDate"] =ToDate.Text;
    dr1["Website"] =TextBox6.Text;
    dt.Rows.Add(dr1); 
    GridView1.DataSource = dt;  //Gridview
    GridView1.DataBind(); //binding data to Gridview
 }

Upvotes: 1

Vipul
Vipul

Reputation: 356

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
    <Columns>
        <asp:BoundField HeaderText="Name" DataField="Name" />
        <asp:BoundField HeaderText="Address" DataField="Address"/>
        <asp:BoundField HeaderText="Contact" DataField="Contact" />
        <asp:BoundField HeaderText="From Date" DataField="FromDate" />
        <asp:BoundField HeaderText="To Date" DataField="ToDate" />
        <asp:BoundField HeaderText="Website"  DataField="Website"/>
    </Columns>
</asp:GridView>

Place this code in your button click event:

{    
    DataTable dt = new DataTable();
    dt.Columns.Add("Name");
    dt.Columns.Add("Address");
    dt.Columns.Add("Contact");
    dt.Columns.Add("FromDate");
    dt.Columns.Add("ToDate");
    dt.Columns.Add("Website");

    DataRow dr1 = dt.NewRow();

    dr1["Name"] = TextBox1.Text;
    dr1["Address"] = TextBox2.Text;
    dr1["Contact"] = TextBox3.Text;
    dr1["FromDate"] = FromDate.Text;
    dr1["ToDate"] = ToDate.Text;
    dr1["Website"] = TextBox6.Text;

    dt.Rows.Add(dr1);
    GridView1.DataSource = dt;  
    GridView1.DataBind(); 
}

This is working piece of code.

Upvotes: 2

Related Questions