Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider there is a datagridview with some data.I need to get the row index for particular value.Not selected index.Please consider that value I mentioned earlier is in a variable.I am a beginner for C#.
Posted
Updated 9-Dec-12 22:32pm
v2
Comments
pradiprenushe 10-Dec-12 5:01am    
What is your use case? Where & when you requiring index?

C#
for(int rowIndex = 0; rowIndex<dataGridView1.Rows.Count; rowIndex++)
        for (int columnIndex = 0; columnIndex < dataGridView1.ColumnCount; columnIndex++)
        {
                   if (dataGridView1[columnIndex, rowIndex].Value.ToString() == textBox2.Text)
                                return rowIndex;
        }
 
Share this answer
 
Comments
NISHAN SANDEEPA 10-Dec-12 4:40am    
Thank you so much Mr Krunal
[no name] 10-Dec-12 4:44am    
If you're satisfied with the given answer, Accept the Solution :)
NISHAN SANDEEPA 10-Dec-12 5:06am    
definitely satisfied Krunal.I hope an answer especially from you.But I didn't mentioned that
[no name] 10-Dec-12 5:08am    
Why especially from me >
NISHAN SANDEEPA 10-Dec-12 8:06am    
Because your answers are clear for beginners like me.Thank you....
Hi Nishan,

Please use below code in Row Command Event

GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
int RId = Convert.ToInt32(GridBU.DataKeys[row.RowIndex].Value);

Then You can get a Row index in DataGrid

Thanks
Mohan
 
Share this answer
 
Comments
NISHAN SANDEEPA 10-Dec-12 4:40am    
Thank you so much Mr Mohan
C#
string strCode = "Hello";
           int iRowindex = -1;
           for (int i = 0; i < Dgv.Rows.Count; i++)
           {


               if (strCode == Convert.ToString(Dgv.Rows[i].Cells[1].Value))
               {
                   Dgv.CurrentCell = Dgv.Rows[i].Cells[1];
                   Dgv.Rows[i].Selected = true;
                   iRowindex = Dgv.Rows[i].Index;
                   //although i and irowindex will be same


               }

           }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900