Getting a primary key from a datagrid. C#

Posted on 2004-08-26
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!
Question by:sean-keys
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

Expert Comment

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.

Expert Comment

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;

Author Comment

ID: 11960282

      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.

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.


Accepted Solution

IceColdas earned 125 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];
string primaryKeyValue = dataGrid1[rowIndex][PKcolumn].ToString();

Expert Comment

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; }
return columnPosition;

Author Comment

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'


Expert Comment

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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