Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
GeneralRe: Track two types of values within one variable Pin
SledgeHammer0127-Jul-12 12:36
SledgeHammer0127-Jul-12 12:36 
GeneralMessage Removed Pin
27-Jul-12 13:52
professionalN_tro_P27-Jul-12 13:52 
GeneralRe: Track two types of values within one variable Pin
jschell28-Jul-12 6:33
jschell28-Jul-12 6:33 
AnswerRe: Track two types of values within one variable Pin
Trak4Net27-Jul-12 18:26
Trak4Net27-Jul-12 18:26 
GeneralRe: Track two types of values within one variable Pin
Paul Conrad28-Jul-12 9:32
professionalPaul Conrad28-Jul-12 9:32 
AnswerRe: Track two types of values within one variable Pin
Northcodedotno29-Jul-12 8:36
Northcodedotno29-Jul-12 8:36 
AnswerRe: Track two types of values within one variable Pin
BobJanova29-Jul-12 23:16
BobJanova29-Jul-12 23:16 
GeneralAdding databound ComboBox into cells of a DataGridView Pin
Dewald27-Jul-12 2:11
Dewald27-Jul-12 2:11 
I have a DataGridView on a form which is populated through databinding from a table in my database. I've followed, more or less, the process outlined on this MSDN page[^]

I've implemented the databinding both ways so that, by clicking on an "Update" button, the changes that have been made on the DataGridView is stored back into the DB. All works well, but...

Imagine the table used for the databinding being something like this:
USERS
- UserID (INT)
- UserName (VARCHAR)
- UserType (INT)


And here's the problem, that UserType field references the identifier in a separate table like this one:
USERTYPES
- UserType (INT)
- UserTypeDescription (VARCHAR)


Currently, my databinding is done with a simple query like this:
SQL
SELECT * FROM USERS;


The problem is that the value displayed in the third column is an integer which means nothing to the end user. If I wasn't interested in two way databinding but only wanted to display the contents of the table in the DataGridView I could easily have overcome that problem by using the following query:
SQL
SELECT UserID, UserName, UserTypeDescription FROM USERS INNER JOIN USERTYPES ON USERS.UserType = USERTYPES.UserType;


Unfortunately I need for the end user to be able to change that value and I'd like to put a ComboBox in all the cells of that column which is populated from the USERTYPES table.

Would really appreciate some advice.

***EDIT***

Maybe I should add that I have some experience with adding a ComboBox (rather a DataGridViewComboBoxCell) in a DataGridView but that has always been to DataGridViews that have not been databound to a table.

Here's how I would've done that:
C#
private void myDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    for (int row = 0; row < (sender as DataGridView).Rows.Count; row++)
    {
    	// Create a new DataGridViewComboBoxCell that will replace the existing UserType cell in the relevant column
        DataGridViewComboBoxCell newCell = new DataGridViewComboBoxCell();
        
        // Populate the DataSource of the combo box
        using (SqlCommand sqlCommand = new SqlCommand("SELECT UserType, UserDescription FROM UserTypes", mySQLConnection))
        {
            using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
            {
                DataSet ds1 = new DataSet();
                DataTable dt1 = new DataTable();
                ds1.Tables.Add(dt1);
                ds1.Load(sqlDataReader, LoadOption.PreserveChanges, ds1.Tables[0]);
                newCell.DataSource = ds1.Tables[0];
                newCell.DisplayMember = "GroupName";
                newCell.ValueMember = "GroupID";
            }
        }
        
        // Set value of combo box to the current value of the UserType cell
        newCell.Value = myDataGridView.Rows[row].Cells["UserType"].Value;
        
        // Replace existing UserType cell with the newly created combo box cell
        myDataGridView.Rows[row].Cells["UserType"].Value = newCell;
    }
}


That final line of code generates an ugly exception which I can only assume is because the DataGridView is bound to a table in the DB because in other projects I've used this code successfully.

The exception that I get is:
Unable to cast object of type 'System.Windows.Forms.DataGridViewComboBoxCell' to type 'System.IConvertible'.Couldn't store <DataGridViewComboBoxCell { ColumnIndex=-1, RowIndex=-1 }> in ConnectGroupID Column.  Expected type is Int32.


modified 27-Jul-12 8:37am.

QuestionHow to make GRIDVIEW as Rounded Corners Pin
mukusingh27-Jul-12 0:33
mukusingh27-Jul-12 0:33 
AnswerRe: How to make GRIDVIEW as Rounded Corners Pin
Richard MacCutchan27-Jul-12 1:57
mveRichard MacCutchan27-Jul-12 1:57 
AnswerRe: How to make GRIDVIEW as Rounded Corners Pin
AmitGajjar27-Jul-12 17:54
professionalAmitGajjar27-Jul-12 17:54 
Questionrefresh result in MDI child Pin
Jassim Rahma27-Jul-12 0:05
Jassim Rahma27-Jul-12 0:05 
AnswerRe: refresh result in MDI child Pin
Eddy Vluggen27-Jul-12 0:09
professionalEddy Vluggen27-Jul-12 0:09 
GeneralRe: refresh result in MDI child Pin
Jassim Rahma27-Jul-12 0:28
Jassim Rahma27-Jul-12 0:28 
GeneralRe: refresh result in MDI child Pin
Eddy Vluggen27-Jul-12 0:47
professionalEddy Vluggen27-Jul-12 0:47 
GeneralRe: refresh result in MDI child Pin
Jassim Rahma27-Jul-12 2:52
Jassim Rahma27-Jul-12 2:52 
GeneralRe: refresh result in MDI child Pin
Eddy Vluggen27-Jul-12 2:59
professionalEddy Vluggen27-Jul-12 2:59 
QuestionHow can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Member 964871826-Jul-12 23:57
Member 964871826-Jul-12 23:57 
AnswerRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Eddy Vluggen27-Jul-12 0:52
professionalEddy Vluggen27-Jul-12 0:52 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Member 964871827-Jul-12 1:54
Member 964871827-Jul-12 1:54 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Eddy Vluggen27-Jul-12 1:59
professionalEddy Vluggen27-Jul-12 1:59 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Dave Kreskowiak27-Jul-12 2:13
mveDave Kreskowiak27-Jul-12 2:13 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Member 964871827-Jul-12 3:54
Member 964871827-Jul-12 3:54 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Dave Kreskowiak27-Jul-12 15:28
mveDave Kreskowiak27-Jul-12 15:28 
QuestionStrange behaviour on C# app in Windows 7... Pin
shiokbarnabas26-Jul-12 20:09
shiokbarnabas26-Jul-12 20:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.