Skip to content

Data Table

Ilkin East

Ilkin East

Asked Apr 7, 2012, 9:50 PM EDTSolved
Share
LinkedInX
Hello

In my Application I have two textbox and datagridview.My datagridview datasource is Data table.I add rows to table from textbox.My problem is when I add different data to table there is only one row.Every time when I second row it removes first.So how to add first,second,third rows to table and show on datagridview?

thanks in advance.

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    ✓ Accepted answer0
    Hi,

    As per as my knowledge is concerned, yes you are right! you need to store in any one of them...
    0
  • Senthilkumar
    0
    Hi,

    The reason is i think you have not stored the data some where and need to add from the stored data.

    I have done the example page. You can change the field name or design.

    Design page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddRows.aspx.cs" Inherits="Corner_AddRows" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
       
       
           
               
                   
                        Enter the First Name:
                   
                   
                       
                   
               
               
                   
                        Enter the Last Name:
                   
                   
                       
                   
               
               
                   
                       
                   
               
               
                   
                       
                            Width="544px">
                           
                           
                           
                           
                           
                           
                           
                       
                   
               
           
       
       


    Code File:

       protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["UsersTable"] != null)
            {
                DataTable dt = (DataTable)Session["UsersTable"];
            }
            
        }
        
        private void BindTable(DataTable dt)
        {
            gvList.DataSource = dt;
            gvList.DataBind();
            Session["UsersTable"] = dt;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dtCurrent = null;

            if (Session["UsersTable"] != null)
            {
                dtCurrent = (DataTable)Session["UsersTable"];
            }
            else
            {
                dtCurrent = CreateTable();
            }

            DataRow dRow = dtCurrent.NewRow();
            dRow["FName"] = txtFirstName.Text.Trim();
            dRow["LName"] = txtLastName.Text.Trim();
            dtCurrent.Rows.Add(dRow);
            BindTable(dtCurrent);

        }

        private DataTable CreateTable()
        {
            DataTable dtable = new DataTable();
            DataColumn dColumn = null;
            DataRow dRow = null;

            dColumn = new DataColumn();
            dColumn.ColumnName = "FName";
            dtable.Columns.Add(dColumn);

            dColumn = new DataColumn();
            dColumn.ColumnName = "LName";
            dtable.Columns.Add(dColumn);

            return dtable;
        }

    Hope this will help you.



    0
  • Ilkin East
    0
    Thank You Senthilkumar!!

    Everything is ok.What if we try to do it in windows application.I need to store it anyware.In a file,xml,database?

    Thanks .

     
    0