473,729 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to get the value from datagridview to textbox in c#

8 New Member
how to get the value from datagridview to textbox on c#?
im a beginner in c#.. and im having a hard time searching for the error free code for this.. need help!!!!
Nov 22 '11 #1
13 43980
arie
64 New Member
To retrive text from one of DataGridView's cells:
Expand|Select|Wrap|Line Numbers
  1. string txt = (string)myDataGridView.Rows[rowindex].Cells[columnindex].Value;
where rowindex is, as the name says integer value of row index, and columnindex is integer value of collumn index of the cell you want to get.

We cast it on string to be safe (I don't remember if it'll work without the cast).

columnindex can also be a string. So, if you have a column that has a Name property set to "myColumn", you can do something like this:
Expand|Select|Wrap|Line Numbers
  1. string txt = (string)myDataGridView.Rows[rowindex].Cells["myColumn"].Value;
This way is less confusing if you have a lot of columns, or if they can be rearranged when program is running (property AllowUserToOrde rColumns of DataGridView).

Now, you can just set Text property of your TextBox:
Expand|Select|Wrap|Line Numbers
  1. myTextBox.Text = txt;
Nov 22 '11 #2
marz javier
8 New Member
...what if i want to select the whole row, not just the current cell??
will i put your code on the currentcellcont ent of datagridview method..?
Nov 23 '11 #3
arie
64 New Member
You can select current cell for example by clicking it - by subscribing to one of this 4 events (pick the one that suits you best):

- CellClicked http://msdn.microsoft.com/en-us/libr...cellclick.aspx
- CellContentClic k http://msdn.microsoft.com/en-us/libr...tentclick.aspx
- CellContentDoub leClicked http://msdn.microsoft.com/en-us/libr...ubleclick.aspx
- CellDoubleClick ed http://msdn.microsoft.com/en-us/libr...ubleclick.aspx

Or you can use the property CurrentCell and CurrentCellChan ged event to update your TextBox http://msdn.microsoft.com/en-us/libr...llchanged.aspx

There are many possibilities, you just have to read through it and choose the one that you like best :)

There is also a property CurrentRow in DataGridView. But I'm afraid that you'll have to get values of its cells one by one, e.g. lets assume you have 5 columns you want to display in one TextBox, separated by spaces:

Expand|Select|Wrap|Line Numbers
  1. string txt = (string)myDataGridView.CurrentRow.Cells["myColumn1"].Value;
  2. txt += " " + (string)myDataGridView.CurrentRow.Cells["myColumn2"].Value;
  3. txt += " " + (string)myDataGridView.CurrentRow.Cells["myColumn3"].Value;
  4. txt += " " + (string)myDataGridView.CurrentRow.Cells["myColumn4"].Value;
  5. txt += " " + (string)myDataGridView.CurrentRow.Cells["myColumn5"].Value;
  6. myTextBox.Text = txt;
Nov 23 '11 #4
marz javier
8 New Member
it didnt work as well.. i cant figure out why.. ;(
Nov 24 '11 #5
arie
64 New Member
What exactly is not working? Can you give a code example?
Nov 24 '11 #6
marz javier
8 New Member
Expand|Select|Wrap|Line Numbers
  1.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  2.         {
  3.  
  4.             string id, name, price, desc, quan;
  5.             id = Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value);
  6.             name = Convert.ToString(dataGridView1.SelectedRows[0].Cells[1].Value);
  7.             price = Convert.ToString(dataGridView1.SelectedRows[0].Cells[2].Value);
  8.             desc = Convert.ToString(dataGridView1.SelectedRows[0].Cells[3].Value);
  9.             quan = Convert.ToString(dataGridView1.SelectedRows[0].Cells[4].Value);
  10.             txtID.Text = id;
  11.             txtName.Text = name;
  12.             txtPrice.Text = price;
  13.             txtDesc.Text = desc;
  14.             txtQuan.Text = quan;
  15.  
  16.         }
  17.  
everytime i clicked the [0][0] cells, im not retrieving all the records in that row..
Nov 25 '11 #7
marz javier
8 New Member
..thank you for a helping hand arie.. i really appreciate your help..
Nov 25 '11 #8
arie
64 New Member
I assume it works for every row except 0?

When I worked with DataGridViews I noticed that sometimes there are some strange problems when you use SelectedRows property.

Also DGW sort of creates cells "on the fly", I don't really understand the mechanism behind it, but sometimes some clells aren't accessible. Although, I never experienced this problem in CellContentClic k event.

If you're using CellContentClic k event, you may want to get the row you clicked this way:

Expand|Select|Wrap|Line Numbers
  1. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  2. {
  3.    if (e.RowIndex>-1 && e.ColumnIndex>-1) // it's row header and column header, trying to access it may cause errors
  4.    {
  5.             string id, name, price, desc, quan;
  6.             id = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
  7.             name = Convert.ToString(dataGridView1.SelectedRows[e.RowIndex].Cells[1].Value);
  8.             price = Convert.ToString(dataGridView1.SelectedRows[e.RowIndex].Cells[2].Value);
  9.             desc = Convert.ToString(dataGridView1.SelectedRows[e.RowIndex].Cells[3].Value);
  10.             quan = Convert.ToString(dataGridView1.SelectedRows[e.RowIndex].Cells[4].Value);
  11.             txtID.Text = id;
  12.             txtName.Text = name;
  13.             txtPrice.Text = price;
  14.             txtDesc.Text = desc;
  15.             txtQuan.Text = quan;
  16.    }
  17. }
Also, had you tried using column names instead fo indexes when you select the cell?

You may also want to check if what you're accessing exist:
Expand|Select|Wrap|Line Numbers
  1. object value = DataGridView1.Rows[rowindex].Cells[columnindex].Value;
  2.     if (value is DBNull) { return; }
Nov 25 '11 #9
marz javier
8 New Member
..but why is everytime i run the program, i got this run time error..
Expand|Select|Wrap|Line Numbers
  1. index was out of range: must be non-negative and less than the size of collection.. how can i solve this error?
  2.  
Nov 29 '11 #10

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

Similar topics

5
2297
by: velu | last post by:
Problem in getting value from textbox & radiobuttonlist to a valuable inside a datagid. I want to insert a record into a table thru datagrid. Here is the code (see below) Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.ItemCommand
1
5099
by: rudranee | last post by:
Hi everyone, I want to know how can i edit a value in textbox in html . I have a field where I have already put a value from database but i also want to edit it . for example ,if in a textbox i put a value 'abc' set in the value attribute <inptu type=text value="<%= var1%>"> .now I want to change the value in the textbox and add it to database. hoe should I do it.
0
2492
by: sunny076 | last post by:
Hi, I am having difficulties creating a derived class for DisabledDataGridViewTextBoxColumn and cell when the VisualStyle is not supported. Basically I am down to either using ControlPaint or Graphics to achieve this. But with Graphics, I am not sure how I can make them diabled. Below are my codes:
5
13535
by: SKS | last post by:
hi all i would like to know how to restrict user from entering numeric value ina textbox..using C# if user try to enter text it shouldnt be allowed.. i tried errror provider.. but it doesnt met my requirement.. thanks in advance for ur valuable reply let me know the best to do it urs
6
3150
by: Chris | last post by:
I created a very simple web page in asp.net. there are only a textbox and a button on the page. when the page loads into web browser there is current date in the textbox. but the date can be changed by user as well. now, when you press the button the date should be inserted into a database. the problem is that , yes it has been inserted, but it is always the current date. even if a user modified the date, it would be inserted the current...
1
1885
by: abirami elango | last post by:
Hi, i have created a web application in vb.net. i have assigned a value to the textbox during page UNLOAD event as below.. ......... Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload txtPopConfirm.Text = intCountAnswer1 End Sub
1
2333
by: adepvivek | last post by:
Hi everybody, I am using textbox in gridview as template. And I want to stored the value of textbox. For that I want to pass the textbox value to stored procedure. So please give me solution.
5
10143
oranoos3000
by: oranoos3000 | last post by:
hi I want to send value of textbox without using form tag and submit button to another page? Is it possible with php ? How do I get this thing work? thanks alot
2
5111
by: s k parimal | last post by:
How to validate decimal value in textbox using C#? I am using this (^(*|\d*\d{1}?\d*)$) but it not working help
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.