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!!!!
13 43975
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 AllowUserToOrde rColumns 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 currentcellcont ent 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 CellContentClic k event.
If you're using CellContentClic k 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?
-
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 DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.ItemCommand
|
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.
|
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:
|
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
|
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...
| |
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
|
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: 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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |