Solved

Getting a primary key from a datagrid. C#

Posted on 2004-08-26
7
282 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
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.
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 …
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…

770 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