Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2008 C# .NET
<b>Hello,
my problem is that,i want get values from selected row in datagridview.
 
please solve this problem</b>
Posted 1-Mar-12 9:10am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Not sure what you're trying to achieve but problebly you should have a look at the Validating Data in the Windows Forms DataGridView Control[^] example on msdn. It gives a nice example of data binding and validation. From that you can extend it, if needed.
 
Regards
 
Piet
  Permalink  
Comments
sunilsmaarth at 1-Mar-12 23:10pm
   
thanks for your reply,
actually i have added one check box in datagridview , if someone check checked box then i want get no. of rows that he has selected.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
To get values from selected row we use :
 
X = dataGridView1[Column,Row].Value;
X needs to be the variable of the same type as value of the cell in the grid.
 
If you want to know what is the number of row that you have selected use this :
 
int Row = dataGridView1.CurrentRow.Index;
 
Number of column in the row starts from zero, so if you have three columns :
 
int X1 = 0;
int X2 = 0;
int X3 = 0;
int Row = 0;
 
Row = dataGridView1.CurrentRow.Index;
X1 = dataGridView1[0,Row].Value;
X2 = dataGridView1[1,Row].Value;
X3 = dataGridView1[2,Row].Value;
 
MessageBox.Show("Values in the " + Row.ToString() + ". row : " + X1.ToString()+
                      " " + X2.ToString() + " " + X3.ToString(), "Values");
 
 
 
If you would like to know many more things about DataGridView control use this link :
 

DataGridView MSDN Library[^]
 
All the best
Perić Željko
  Permalink  
v7
Comments
sunilsmaarth at 1-Mar-12 23:12pm
   
thanks for your reply,
actually i have added one check box in datagridview , if someone check checked box then i want get no. of rows that he has selected.
Perić_Željko at 2-Mar-12 8:35am
   
Solution for your problem is complicated or not functional. It is because of logic of using DataGridview control. If you have within DataGridView control CheckBoxColumn, and you want to get information on how many rows where selected after checking checkbox it is impossible because of losing focus on selected rows (selected rows become deselected) and info of how many rows where selected , after clicking on checkbox is lost.
 
But, there is a way by connecting two events one is that user have checked checkbox first and the other is MouseUp event after selecting rows.
 
So if you connect these two events you can get something like this :
 
void DataGridView1MouseUp(object sender, MouseEventArgs e){
 
// TODO: Implement DataGridView1MouseUp
 
if ( dataGridView1[Column,Row].Value.ToString() == "True" ) {
 
MessageBox.Show(dataGridView1.SelectedRows.Count.ToString()); }}
 
Make sure that instead of Column and Row it is necessary to enter the coordinates of the cell that contains a check box.
 
Now the logic is the following, the user checks first the check box and then starts to select rows, by releasing the mouse button (DataGridView1MouseUp event) the program throws a message with the number of selected rows.
 
Dysfunction of the program is that you release the button each time when you clicks.So the event MouseUp occurs each time mouse button is clicked.
 
Perhaps you could find different solution for your problem.
shaikh-adil at 6-Jan-13 5:46am
   
http://www.codeproject.com/Questions/522502/passingplustheplusdataplusfromplustextboxplustoplu
can you help me sir
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can get the selected rows with the DataGridView's .SelectedRows property.
 
See this MSN Link[^].
 
So, for example, if you only allowed one row to be selected at a time and if you wanted only the value of a certain column, you'd access it like this:
Dim strValueYouWant As String = String.Empty
If dgv.SelectedRows.Count > 0 Then
   strValueYouWant = dgv.SelectedRows(0)("CertainColumn").Value
End If
(This is in VB, but you should be able to translate it pretty easily)
  Permalink  
Comments
sunilsmaarth at 1-Mar-12 23:11pm
   
thanks for your reply,
actually i have added one check box in datagridview , if someone check checked box then i want get no. of rows that he has selected.
Kschuler at 2-Mar-12 9:34am
   
If all you want is the number of rows selected, then use:
Dim intNumberSelectedRows as Integer = dgv.SelectedRows.Count
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Dim xxx As String = Me.DataGridView1.SelectedRows(0).Cells(0).Value.ToString
MsgBox(xxx)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Private Sub dgvPE_CellMouseDoubleClick(sender As Object, e _ 
As DataGridViewCellMouseEventArgs) Handles dgvPE.CellMouseDoubleClick
   select_ID = Convert.ToInt32(dgvPE.CurrentRow.Index)'get selected row index
   select_ID = Convert.ToInt32(dgvPE.Rows(select_ID).Cells(0).Value) 'get selected value
   MessageBox.Show(select_ID)
End Sub
  Permalink  
v2
Comments
Nelek at 16-Oct-14 4:27am
   
Did you noticed that this is a 2,5 years old question, that already has been solved with an accepted solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 273
2 Aajmot Sk 234
3 Marcin Kozub 205
4 Richard MacCutchan 200
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 16 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100