473,511 Members | 16,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert dat to datagrid view from twxt boxes

5 New Member
if there are 3-4 textboxes diplaying some calcuted value,then how can we add these values to datagrid view and database
Jul 22 '10 #1
5 2885
maddyromeo
22 New Member
you can add textbox values to a datagridview by just

1)create a "DataTable"
2)create a DataColumn
3)Add DataColumn to DataTable
4)create a new DataRow
5)Append textBox value to DataRow
6)Add DataRow to DataTable
7)set datasource of datagridview to DataTable


sample code.....

1)DataTable dt = new DataTable();
2)DataColumn values = new DataColumn("values",typeof(System.String));
3)dt.Columns.Add(values);
4)DataRow dr = dt.NewRow();
5)dr["values"] = textBox1.Text;
6)dt.Rows.Add(dr);
7)dataGridView1.DataSource = dt;
Jul 22 '10 #2
shweta134
5 New Member
Sir thanx above code is working fine but i want to save 5 to 10 values to the same datagrid view ,each value until i close the window
Jul 23 '10 #3
maddyromeo
22 New Member
if you understand the above code you can figure out the answer yourself.............

if you want to add the next value to a new row of dagaGridView , just create a new datarow as shown before

/*
dr = dt.NewRow();
dt["columnName"] = "the string you want to insert";
dt.Rows.Add(dr);
*/ this code will add a new row to your dataGridView
with the given string.


in order to add new values to your dataGridView you have to code in such a way that the above code executes each time you want to insert a string.....
Jul 23 '10 #4
maddyromeo
22 New Member
... in case if you want to add your values to new columns the just create new columns

values = new DataColumn("values1",typeof(System.String));
dt.Columns.Add(values);

values = new DataColumn("values2",typeof(System.String));
dt.Columns.Add(values);

and now create row like

string[] row = {""+textBox1.Text,""+textBox2.Text}
dt.Rows.Add(row);

for more values add more columns and then add values in

string[] row = {""+textBox1.Text,""+textBox2.Text+,.....}
Jul 23 '10 #5
sdmunoz
1 New Member
Given two textboxs (textbox1 and textbox2), you can add it content to datagridview:

Expand|Select|Wrap|Line Numbers
  1. dt.Rows.Add(textbox1.Text, textbox2.Text);
Obviously you had to have the two columns added in the datagridview control. Then to save it to database (I'll omit connections and queries):

Expand|Select|Wrap|Line Numbers
  1. SqlCommand command = new SqlCommand();
  2. if (dt.Rows.Count > 0)
  3. {
  4.    for (int i = 0; i < dt.Rows.Count; i++)
  5.    {
  6.       command.CommandText = "INSERT INTO table(val1,val2) VALUES(@val1,@val2)";
  7.       command.Parameters.AddWithValue("@val1", dt.Rows[i].Cells[0].Value.ToString());
  8.       command.Parameters.AddWithValue("@val2", dt.Rows[i].Cells[1].Value.ToString());
  9.       command.ExecuteNonQuery();
  10.    }
  11. }
  12.  
Jul 24 '10 #6

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

Similar topics

5
7086
by: ComputerStop | last post by:
I am attempting to print a datagridview. I have not found any method that works successfully. Has any one been successful with this?
2
2270
by: Bob | last post by:
I looked at the sample in the msdn january 2005 - Introducing a new datagrid, but the project downloaded failed to open. Any one can point me to a bit of code on how to go and select an image...
0
1188
by: shukyh | last post by:
Hi all I would like to generate an HTML page from datagridview in win application (not webform). In th datagrid view I have DataGridViewCheckBoxColumn columns. How Can i do this? Thanks, Shuky
0
1101
by: Tony A. | last post by:
I'm using VB 2005 with MS Access for as the database. I have a table (tblItem) that has two columns, ItemNum (key field) and ItemDescription. I also have a second table (tblOrdersDetail), this...
1
2583
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
How do I get the value of a checkbox in a row in a datagrid view please? I have tried many methods and nothing workds. This was so much easier in VB 6.0. THanks for any help you can give me.
1
1735
by: radhikabista | last post by:
hey friends , i m not being able to save the updates in datagrid view when i press buttonsave_ gridview i have a class customer with two methods one to get the dataset and other to update database:...
1
1523
by: radhikabista | last post by:
i have a table tbl_client in Ms Access database. my program displays a datagrid view with the search result records. i want to save the changes to data gridview to database.for this i call the...
1
3812
mafaisal
by: mafaisal | last post by:
Hello Experts I am Using vb.net 2005 Hw To Put DateTime Picker in Datagrid view I have to sea Datetime Picker in Specified Cilomn of Datagrid I try This But The Location Hw To Change to the...
1
1349
by: menyki | last post by:
pls i designed a database application using visual basic. i have done the coding for the print incase i want to print out the data. Pls my problem is how to code at design time for the...
1
4840
by: eihabisaac | last post by:
Hey All i need a usefull link to help me update, delete, insert and select data using a datagrid view. and if there is a way to put the delete and update button in the same row. i already...
0
7423
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
7510
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...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.