Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting a primary key from a datagrid. C#

Posted on 2004-08-26
7
Medium Priority
?
291 Views
Last Modified: 2010-04-17
Right now I have a program where a user needs to highlight a row and click a button.   The function of that button depends on the row selection.   Is there a way to sync the CurrentRowIndex with a cell in that row like the primary key cell for example, or is there a completely different way to do this?  I'm open to suggestions!
Thanks
Sean
0
Comment
Question by:sean-keys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 2

Expert Comment

by:dimitry_dimitry
ID: 11910517
CurrentRowIndex tells you a position in your dataset, which is connected with grid.
m_Grid.DataSource - which is typically DataSet or DataTable - .DataSet.Tables(0).Rows(m_Grid.DataSource.CurrentRowIndex)
where m_Grid is your grid.
0
 
LVL 3

Expert Comment

by:IceColdas
ID: 11910665
supose you know the position of the primary key column

int rowIndex = dataGrid1.CurrentRowIndex;
dataGrid1[rowIndex][PKcolumn]  will return the value of the desired column, from the current row;
0
 

Author Comment

by:sean-keys
ID: 11960282
IceColdas

      It sounds like you hit the nail on the head, is there anyway you can give a brief example with a little more detail.  Please forgive my ignorance, sorry for the delay.

Thanks,
Sean
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Accepted Solution

by:
IceColdas earned 500 total points
ID: 11961146
ok, lets say you have this command string : commandString = "SELECT primaryKey, column2, column3, column4 FROM table1"
In this case, the primary Key column will be on the first position (means position 0).
else, if commandString = "SELECT column1, column2, primaryKey,  column4 FROM table1" (in this case, PKcolumn = 2)
Therefor, the program will be :

int PKcolumn = 0; //first position is 0, second position = 1, and so on
int rowIndex = dataGrid1.CurrentRowIndex;
//now, depending on the type of primaryKey field, either
int primaryKeyValue = (int)dataGrid1[rowIndex][PKcolumn];
//or
string primaryKeyValue = dataGrid1[rowIndex][PKcolumn].ToString();
0
 
LVL 3

Expert Comment

by:IceColdas
ID: 11961370
second, you can find any column position from the datagrid tablestyles, if you know the name of that column;
this way, if the column position change in the database, then your program will still work
i.e. :
private int GetColumnPosition(string myColumnName)
{
//myColumnName the name of the column you want to get the position
int columnPosition = -1;
int i=0;
/* either use TableStyles[0] if you have only one table in the dataset, or TableStyles[tableName] to specify the table name */
foreach (System.Windows.Forms.DataGridColumnStyle cs in dataGrid1.TableStyles[0].GridColumnStyles)
{
      /*for case insensitive comparision:
                                                 if  (cs.MappingName.ToUpper() == myColumnName.ToUpper() ) */
                if (cs.MappingName == myColumnName)
                 { columnPosition = i; break; }
                i++;
}
return columnPosition;
}
0
 

Author Comment

by:sean-keys
ID: 11986602
I get the following errors when I build the code.   Maybe someone can shed some light on what I over looked.  Thanks

private void test_Click(object sender, System.EventArgs e)
            {
                                    
                  int PKcolumn= 0;
                  int rowIndex = grdcustomers.CurrentRowIndex;
                  int primaryKeyValue= (int) grdcustomers[rowIndex][PKcolumn];
                  testval.Text =primaryKeyValue.ToString() ;
                  
                  
            }

(1606): The best overloaded method match for 'System.Windows.Forms.DataGrid.this[System.Windows.Forms.DataGridCell]' has some invalid arguments
(1606): Argument '1': cannot convert from 'int' to 'System.Windows.Forms.DataGridCell'


0
 
LVL 3

Expert Comment

by:IceColdas
ID: 11988085
sorry, my mistake
it is
 int primaryKeyValue= (int) grdcustomers[rowIndex, PKcolumn];
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Simple Linear Regression

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question