By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

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

P: 18
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="#333333" CssClass

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

OnRowDeleting="GVWriteDR_RowDeleting" OnRowEditing="GVWriteDR_RowEditing"

OnSelectedIndexChanged="GVWriteDR_SelectedIndexCha nged" OnRowDataBound="GVWriteDR_RowDataBound"

AutoGenerateColumns="False">
<FooterStyle BackColor="#507CD1" Font-Bold ="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor ="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:ButtonField Text="Edit" />
<asp:ButtonField Text="Delete" />
<asp:BoundField DataField="EName" HeaderText="EName" />
<asp:BoundField DataField ="Dept" HeaderText ="Dept" />
<asp:BoundField DataField =SlNo." Headertext="SlNo." />
</Columns>
</asp:GridView>


The Code behind for this goes like this:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;

namespace ACMS.aspx_Pages
{
public partial class TestMail : System.Web.UI.Page
{
public string WDRConString = "";
public static DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
WDRConString = ConfigurationManager.AppSettings["ConnectionString"];
GVWriteDR.Visible = true;
dt = new DataTable();
}

protected void btnSave_Click(object sender, EventArgs e)
{
GVWriteDR.DataSource = dt;
GVWriteDR.DataBind();
DataRow dr = dt.NewRow();
dr["EName"] = txtName.Text.ToString();
dr["Dept"] = txtDept.Text.ToString();
dr["SlNo."] = txtSlNo.Text.ToString();

dt.Rows.Add(dr);
GVWriteDR.DataSource = dt;
GVWriteDR.DataBind();
}
}
}

I am getting the runtime error,

System.ArgumentException: Column 'EName' does not belong to table .

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

Please help me in solving this problem.

Thanks in Advance,
Hiranmaie.
Feb 10 '09 #1
Share this Question
Share on Google+
8 Replies


Expert 100+
P: 750
PRR
you need to add the columns to the table before you can add rows...
sample code
Expand|Select|Wrap|Line Numbers
  1. DataTable dt = new DataTable();
  2.         DataColumn dc = new DataColumn("name");
  3.         dt.Columns.Add(dc);
  4.         dc = new DataColumn("field");
  5.         dt.Columns.Add(dc);
  6. //Now your table has defined columns
  7. // you can add rows...
  8.  
  9.         DataRow dr = dt.NewRow();
  10.         dr["Name"] = TextBox1.Text;
  11.         dr["field"] = TextBox2.Text;
  12.  
  13.         GridView1.DataSource = dt;
  14.         GridView1.DataBind();
  15.  
Feb 10 '09 #2

P: 18
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.
Feb 11 '09 #3

Expert 100+
P: 750
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..
Expand|Select|Wrap|Line Numbers
  1. if (Cache["myTable"] != null)
  2.         {
  3.             dt == Cache["myTable"] as DataTable;
  4.         }
  5. else
  6. {
  7. //update the datatable from database 
  8. }
  9.  
  10.         DataRow dr = dt.NewRow();
  11.         dr["Name"] = TextBox1.Text;
  12.         dr["field"] = TextBox2.Text;
  13.  
  14.         Cache["myTable"] = dt;
  15.  
  16.         GridView1.DataSource = dt;
  17.         GridView1.DataBind();
  18.  
If the datatable is null then get values from the database ....
Feb 11 '09 #4

P: 18
Thank you so...soo much. I couldn't get it anywhere from past 3 days.
Thanks again.

Thanks & Regards,
Hiranmaie
Feb 11 '09 #5

Expert 100+
P: 750
PRR
Welcome, do continue to post your queries on Bytes
Feb 11 '09 #6

P: 18
I am having many other doubts, will post it ASAP

Thank you.
Feb 11 '09 #7

P: 1
how can add values into gridview from textbox and not using database(in c sharp) pls answer me
Nov 25 '11 #8

P: 7
@PRR
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.
Nov 25 '11 #9

Post your reply

Sign in to post your reply or Sign up for a free account.