Code to pass textbox values to gridview in C#.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hiranmaie
    New Member
    • Feb 2009
    • 18

    #1

    Code to pass textbox values to gridview in C#.net

    Hi,

    I am having 3 textboxes and 1 gridview.

    I want to pass (reflect) the values from textboxes to gridview.
    I am not using the database here.
    I want to save the data directly from textboxes into the gridview.

    The asp code for the gridview goes like this:

    <asp:GridView ID="GVWriteDR" runat="server" AllowSorting ="True" CellPadding="4" ForeColor="#333 333" CssClass

    ="bsgridview "
    GridLines="None " AllowPaging="Tr ue" align="center" Width="1%" PageSize ="5"

    OnRowDeleting=" GVWriteDR_RowDe leting" OnRowEditing="G VWriteDR_RowEdi ting"

    OnSelectedIndex Changed="GVWrit eDR_SelectedInd exChanged" OnRowDataBound= "GVWriteDR_RowD ataBound"

    AutoGenerateCol umns="False">
    <FooterStyle BackColor="#507 CD1" Font-Bold ="True" ForeColor="Whit e" />
    <RowStyle BackColor="#EFF 3FB" />
    <EditRowStyle BackColor ="#2461BF" />
    <SelectedRowSty le BackColor="#D1D DF1" Font-Bold="True" ForeColor="#333 333" />
    <PagerStyle BackColor="#246 1BF" ForeColor="Whit e" HorizontalAlign ="Center" />
    <HeaderStyle BackColor="#507 CD1" Font-Bold="True" ForeColor="Whit e" />
    <AlternatingRow Style BackColor="Whit e" />
    <Columns>
    <asp:ButtonFiel d Text="Edit" />
    <asp:ButtonFiel d Text="Delete" />
    <asp:BoundFie ld DataField="ENam e" HeaderText="ENa me" />
    <asp:BoundFie ld DataField ="Dept" HeaderText ="Dept" />
    <asp:BoundFie ld DataField =SlNo." Headertext="SlN o." />
    </Columns>
    </asp:GridView>


    The Code behind for this goes like this:


    using System;
    using System.Data;
    using System.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using System.Data.Ole Db;
    using System.Text;

    namespace ACMS.aspx_Pages
    {
    public partial class TestMail : System.Web.UI.P age
    {
    public string WDRConString = "";
    public static DataTable dt;
    protected void Page_Load(objec t sender, EventArgs e)
    {
    WDRConString = ConfigurationMa nager.AppSettin gs["ConnectionStri ng"];
    GVWriteDR.Visib le = true;
    dt = new DataTable();
    }

    protected void btnSave_Click(o bject sender, EventArgs e)
    {
    GVWriteDR.DataS ource = dt;
    GVWriteDR.DataB ind();
    DataRow dr = dt.NewRow();
    dr["EName"] = txtName.Text.To String();
    dr["Dept"] = txtDept.Text.To String();
    dr["SlNo."] = txtSlNo.Text.To String();

    dt.Rows.Add(dr) ;
    GVWriteDR.DataS ource = dt;
    GVWriteDR.DataB ind();
    }
    }
    }

    I am getting the runtime error,

    System.Argument Exception: Column 'EName' does not belong to table .

    at dr["EName"] = txtName.Text.To String();

    Please help me in solving this problem.

    Thanks in Advance,
    Hiranmaie.
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    you need to add the columns to the table before you can add rows...
    sample code
    Code:
    DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("name");
            dt.Columns.Add(dc);
            dc = new DataColumn("field");
            dt.Columns.Add(dc);
    //Now your table has defined columns
    // you can add rows...
    
            DataRow dr = dt.NewRow();
            dr["Name"] = TextBox1.Text;
            dr["field"] = TextBox2.Text;
    
            GridView1.DataSource = dt;
            GridView1.DataBind();

    Comment

    • hiranmaie
      New Member
      • Feb 2009
      • 18

      #3
      Thank you very much for the reply,its working now. :)

      Now i want to insert the multiple row values into the gridview.
      How to code for that so that i retain the previous values in the gridview itself and insert the new value into

      the grid.

      For Eg:

      If I have 3 column, with column headers as - Name and Department.
      I want my gridview to look as below before inserting into the database.


      Name| Department
      ------ |-----------
      ABC | Finance
      XYZ | HR
      AAA | Software


      I want to insert a row value into the grid at teh click of a button, by restoring the previous row values.

      Thanks in advance,
      Hiranmaie.

      Comment

      • PRR
        Recognized Expert Contributor
        • Dec 2007
        • 750

        #4
        Cache the datatable.. when inserting new row.. just copy the cached datatable to your datatable and add a new row.. see to it that the cached datatable is not null..
        Code:
        if (Cache["myTable"] != null)
                {
                    dt == Cache["myTable"] as DataTable;
                }
        else
        {
        //update the datatable from database 
        }
        
                DataRow dr = dt.NewRow();
                dr["Name"] = TextBox1.Text;
                dr["field"] = TextBox2.Text;
        
                Cache["myTable"] = dt;
        
                GridView1.DataSource = dt;
                GridView1.DataBind();
        If the datatable is null then get values from the database ....

        Comment

        • hiranmaie
          New Member
          • Feb 2009
          • 18

          #5
          Thank you so...soo much. I couldn't get it anywhere from past 3 days.
          Thanks again.

          Thanks & Regards,
          Hiranmaie

          Comment

          • PRR
            Recognized Expert Contributor
            • Dec 2007
            • 750

            #6
            Welcome, do continue to post your queries on Bytes

            Comment

            • hiranmaie
              New Member
              • Feb 2009
              • 18

              #7
              I am having many other doubts, will post it ASAP

              Thank you.

              Comment

              • vava123
                New Member
                • Nov 2011
                • 1

                #8
                how can add values into gridview from textbox and not using database(in c sharp) pls answer me

                Comment

                • xunter
                  New Member
                  • Nov 2011
                  • 7

                  #9
                  Originally posted by PRR
                  Cache the datatable.. when inserting new row.. just copy the cached datatable to your datatable and add a new row.. see to it that the cached datatable is not null..
                  Code:
                  if (Cache["myTable"] != null)
                          {
                              dt == Cache["myTable"] as DataTable;
                          }
                  else
                  {
                  //update the datatable from database 
                  }
                  
                          DataRow dr = dt.NewRow();
                          dr["Name"] = TextBox1.Text;
                          dr["field"] = TextBox2.Text;
                  
                          Cache["myTable"] = dt;
                  
                          GridView1.DataSource = dt;
                          GridView1.DataBind();
                  If the datatable is null then get values from the database ....
                  I think that you should use the ViewState collection in situations like above instead Cache because the ViewState is only for each page and the Cache object is for an application.

                  Comment

                  Working...