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!!!!
or, you can try dataGridView1.CellClick - public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
-
}
-
-
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
-
{
-
if (e.RowIndex < 0 || e.ColumnIndex < 0)
-
return;
-
-
string id, name, price, desc, quan;
-
id = dataGridView1.Rows[e.RowIndex].Cells[0].Value + "";
-
name = dataGridView1.Rows[e.RowIndex].Cells[1].Value + "";
-
price = dataGridView1.Rows[e.RowIndex].Cells[2].Value + "";
-
desc = dataGridView1.Rows[e.RowIndex].Cells[3].Value + "";
-
quan = dataGridView1.Rows[e.RowIndex].Cells[4].Value + "";
-
txtID.Text = id;
-
txtName.Text = name;
-
txtPrice.Text = price;
-
txtDesc.Text = desc;
-
txtQuan.Text = quan;
-
}
-
}
13 43887
To retrive text from one of DataGridView's cells: - 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: - 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 AllowUserToOrderColumns of DataGridView).
Now, you can just set Text property of your TextBox:
...what if i want to select the whole row, not just the current cell??
will i put your code on the currentcellcontent of datagridview method..?
it didnt work as well.. i cant figure out why.. ;(
What exactly is not working? Can you give a code example?
-
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
-
{
-
-
string id, name, price, desc, quan;
-
id = Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value);
-
name = Convert.ToString(dataGridView1.SelectedRows[0].Cells[1].Value);
-
price = Convert.ToString(dataGridView1.SelectedRows[0].Cells[2].Value);
-
desc = Convert.ToString(dataGridView1.SelectedRows[0].Cells[3].Value);
-
quan = Convert.ToString(dataGridView1.SelectedRows[0].Cells[4].Value);
-
txtID.Text = id;
-
txtName.Text = name;
-
txtPrice.Text = price;
-
txtDesc.Text = desc;
-
txtQuan.Text = quan;
-
-
}
-
everytime i clicked the [0][0] cells, im not retrieving all the records in that row..
..thank you for a helping hand arie.. i really appreciate your help..
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 CellContentClick event.
If you're using CellContentClick event, you may want to get the row you clicked this way: - private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
-
{
-
if (e.RowIndex>-1 && e.ColumnIndex>-1) // it's row header and column header, trying to access it may cause errors
-
{
-
string id, name, price, desc, quan;
-
id = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
-
name = Convert.ToString(dataGridView1.SelectedRows[e.RowIndex].Cells[1].Value);
-
price = Convert.ToString(dataGridView1.SelectedRows[e.RowIndex].Cells[2].Value);
-
desc = Convert.ToString(dataGridView1.SelectedRows[e.RowIndex].Cells[3].Value);
-
quan = Convert.ToString(dataGridView1.SelectedRows[e.RowIndex].Cells[4].Value);
-
txtID.Text = id;
-
txtName.Text = name;
-
txtPrice.Text = price;
-
txtDesc.Text = desc;
-
txtQuan.Text = quan;
-
}
-
}
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: - object value = DataGridView1.Rows[rowindex].Cells[columnindex].Value;
-
if (value is DBNull) { return; }
..but why is everytime i run the program, i got this run time error.. -
index was out of range: must be non-negative and less than the size of collection.. how can i solve this error?
-
in CellContentClick you have to ensure that the index you're trying to process:
- is non-negative, meaning isn't -1 (index of headers)
- less than the size of collection, meaning that it isn't the index of new row or index isn't greater than the number of rows or columns you actually have
or, you can try dataGridView1.CellClick - public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
-
}
-
-
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
-
{
-
if (e.RowIndex < 0 || e.ColumnIndex < 0)
-
return;
-
-
string id, name, price, desc, quan;
-
id = dataGridView1.Rows[e.RowIndex].Cells[0].Value + "";
-
name = dataGridView1.Rows[e.RowIndex].Cells[1].Value + "";
-
price = dataGridView1.Rows[e.RowIndex].Cells[2].Value + "";
-
desc = dataGridView1.Rows[e.RowIndex].Cells[3].Value + "";
-
quan = dataGridView1.Rows[e.RowIndex].Cells[4].Value + "";
-
txtID.Text = id;
-
txtName.Text = name;
-
txtPrice.Text = price;
-
txtDesc.Text = desc;
-
txtQuan.Text = quan;
-
}
-
}
arie, its solved!! thank you so much..
How are you
How Collect column from data grid view in text box
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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
|
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
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
| |