473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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="#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
8 29995
PRR
750 Expert 512MB
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
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
PRR
750 Expert 512MB
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
Thank you so...soo much. I couldn't get it anywhere from past 3 days.
Thanks again.

Thanks & Regards,
Hiranmaie
Feb 11 '09 #5
PRR
750 Expert 512MB
Welcome, do continue to post your queries on Bytes
Feb 11 '09 #6
I am having many other doubts, will post it ASAP

Thank you.
Feb 11 '09 #7
how can add values into gridview from textbox and not using database(in c sharp) pls answer me
Nov 25 '11 #8
xunter
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

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

Similar topics

4
by: Terry | last post by:
The following code (VB6) automatically sets ticks or null values for Check Boxes: 'Pass' and/or 'Resit', depending on the data entered in the 'ModuleID' and 'Mark' Text Boxes. 'ModuleID' is a...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
2
by: Dabbler | last post by:
I have a TextBox used to enter a search value and a DropDownList used to select which field should be searched on. I need to load a GridView with the search results. I have setup multiple select...
0
by: SDRoy | last post by:
Hi Can someone help me to figure out how to get the editable textbox value from GridView in my code-behind ? I am using the code something like- GridView2.Rows.Cells.Text.Trim(). But when I...
7
by: GaryDean | last post by:
I'm trying to load a GridView up with data manually, in code. I'm not using any datasource. Using this code.... ArrayList myRowArrayList; GridViewRow myGVR = new...
5
by: pbearne | last post by:
Hi guys I am trying to this function below I have a web form with a load of inputs text boxs etc and i need to disable them So I have tried to write a function I can call and get the...
0
geo039
by: geo039 | last post by:
I have a usercontrol with textboxes. Most of which refer to a gridview on selection. A user clicks a looking glass and a gridview appears to select items (like a dropdown). The page loads with...
2
by: | last post by:
I have a gridview with a textbox in an ItemTemplate, as below. The OnTextChanged event fires okay but how do I pass a parameter to it, I get an error when I try "OnTextChanged(""SomeData"")". I...
8
by: tina2626 | last post by:
how can i pass textbox value to gridview using C#.net at runtime not using database values. i m hving textbox1 and gridview1. can anyone suggest me to do this coding in ASP.NET(C# language).
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.