By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,901 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,901 IT Pros & Developers. It's quick & easy.

[C# Win Form] DGV ComboBox Default values/ bind from DB

P: 77
Hi, Im tyring to display a dataGridView with comboBoxes on about 5 of the fields. This table is basically permissions to certain parts of my program. In the database i just store a 1 or a 0 (access or no access). Now obviously to the user i want this to be displayed as text. Now currently i am creating the columns manually and using the cell changed event to do the updating. This is quite long winded and if possible i would like a simpler (and cleaner) way to do this.

Heres a snippet of code for adding the comboBox Columns:
Expand|Select|Wrap|Line Numbers
  1.             DataGridViewComboBoxColumn ddlConfig = new DataGridViewComboBoxColumn();
  2.             ddlConfig.HeaderText = "Config";
  3.             ddlConfig.Items.Add("No Access");
  4.             ddlConfig.Items.Add("Full Access");
  5.             ddlConfig.DisplayIndex = 2;
  6.             dataGridView1.Columns.Add(ddlConfig);
  7.  
And heres a snippet of the updating code:
Expand|Select|Wrap|Line Numbers
  1. string primKey = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
  2.                 if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Config" ||
  3.                     dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Reports" ||
  4.                     dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Groups" ||
  5.                     dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Predefined" ||
  6.                     dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Permissions")
  7.                 {
  8.                     int val = 0;
  9.                     if (dataGridView1.CurrentCell.Value.ToString() == "No Access")
  10.                         val = 0;
  11.                     else if (dataGridView1.CurrentCell.Value.ToString() == "Full Access")
  12.                         val = 1;
  13.                     else
  14.                         val = 0;
  15.  
  16.                     string command = "UPDATE Permissions SET " + dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() + "='" + val + "' WHERE ID = " + primKey;
  17.                     try
  18.                     {
  19.                         conn.Open();
  20.                         SqlCommand cmd = new SqlCommand(command, conn);
  21.                         cmd.ExecuteNonQuery();
  22.                     }
  23.                     catch
  24.                     {
  25.  
  26.                     }
  27.                     finally
  28.                     {
  29.                         conn.Close();
  30.                     }
  31.  
So, the things i want to be able to do are: value already in database is displayed in text form when loading, On the value changing update the database value to 1 and having the text values in the ddl not the int values.

If theres not a simpler way to do this can someone please tell me the best way to set the values for each row... Because currently theres a datasource binded to the dgv and i just add the other columns on (hence the DisplayIndex property).


Many Thanks,
Regards,
Piercy
Mar 20 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.