Solved

Getting a primary key from a datagrid. C#

Posted on 2004-08-26
7
279 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
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];
//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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sorting in Excel with Group Headers if the Exist 2 82
strCount chalenge 3 71
pairs challenge 5 66
Installed softare without GUID 6 66
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now