Windows Develop Bookmark and Share   
 index > Windows Forms General > datakeynames for datagridview
 

datakeynames for datagridview

Hi,


In a web application, you set up the datakeynames for the datagridview in a aspx page.

How and where do you setup a datakeynames for the datagridview in a windows form application?


Thanks in Advance
rowter  Tuesday, September 22, 2009 12:27 AM
Hi,
Ther is no property like datakeynames in windows datagridview.
but we can do some other way with uniqe key operation in datagridview.
create the datacolum and dataset with unique key fied and bind that dataset in datagridview.

 private DataSet CreateDataSet()
        {
            DataTable table = new DataTable("childTable");
            DataColumn column;
            DataRow row;
            DataSet dataset = new DataSet();
            // Create first column and add to the DataTable.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "CustID";
            column.AutoIncrement = true;
            column.Caption = "ID";
            column.ReadOnly = true;
            //Unique Column
            column.Unique = true;
            
            // Add the column to the DataColumnCollection.
            table.Columns.Add(column);

            //// Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "CustName";
            column.AutoIncrement = false;
            column.Caption = "Name";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);

            dataset.Tables.Add(table);

            // Create three sets of DataRow objects, 
            // five rows each, and add to DataTable.
            for (int i = 0; i <= 10; i++)
            {
                row = table.NewRow();
                row["CustID"] = i;
                row["CustName"] = "Item " + i;
                table.Rows.Add(row);
            }
            return dataset;

        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
  • Marked As Answer byrowter Tuesday, September 22, 2009 2:07 PM
  •  
Gnanadurai  Tuesday, September 22, 2009 5:32 AM
Hi,
Ther is no property like datakeynames in windows datagridview.
but we can do some other way with uniqe key operation in datagridview.
create the datacolum and dataset with unique key fied and bind that dataset in datagridview.

 private DataSet CreateDataSet()
        {
            DataTable table = new DataTable("childTable");
            DataColumn column;
            DataRow row;
            DataSet dataset = new DataSet();
            // Create first column and add to the DataTable.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "CustID";
            column.AutoIncrement = true;
            column.Caption = "ID";
            column.ReadOnly = true;
            //Unique Column
            column.Unique = true;
            
            // Add the column to the DataColumnCollection.
            table.Columns.Add(column);

            //// Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "CustName";
            column.AutoIncrement = false;
            column.Caption = "Name";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);

            dataset.Tables.Add(table);

            // Create three sets of DataRow objects, 
            // five rows each, and add to DataTable.
            for (int i = 0; i <= 10; i++)
            {
                row = table.NewRow();
                row["CustID"] = i;
                row["CustName"] = "Item " + i;
                table.Rows.Add(row);
            }
            return dataset;

        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
  • Marked As Answer byrowter Tuesday, September 22, 2009 2:07 PM
  •  
Gnanadurai  Tuesday, September 22, 2009 5:32 AM
Thanks Gnanadurai
  • Unmarked As Answer byrowter Tuesday, September 22, 2009 2:07 PM
  • Marked As Answer byrowter Tuesday, September 22, 2009 2:07 PM
  •  
rowter  Tuesday, September 22, 2009 2:07 PM

You can use google to search for other answers

Custom Search

More Threads

• Autosize of Label is not working
• How to change page number while printing
• trying to handle mouse events for a splitcontainer control
• Binding navigator - reposition.
• Problem with unwanted dialog box from webbrowser control
• refreshing TableAdapter in xsd
• Windows Service & Form...
• How2 - Drag&Drop onto a From without using Controls
• Media Player Look & Feel?
• Why is Left and Right not included in the string class?