473,238 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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

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);
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;
  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.                     {
  26.                     }
  27.                     finally
  28.                     {
  29.                         conn.Close();
  30.                     }
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,
Mar 20 '08 #1
0 1244

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

Similar topics

by: Steve | last post by:
Form FrmRestock's recordsource is QryFrmRestock. The TransactionDate field's criteria is set ats: Forms!FrmRestock!LastXDays. LastXDays on the form is a combobox where the selections are 30, 60...
by: Tamir Khason | last post by:
Why this does not work??? cb1 - combobox DataProvider.DataProvider.DS_BringRacks() - DataSet cb1.DataSource=DataProvider.DataProvider.DS_BringRacks().Tables; cb1.DisplayMember = "id"; ...
by: NCrum | last post by:
I want to set the Default value of a Combobox for any changeable record and have got this working but it is totaly unsatisfactory see the code below I loop through the items in the Combo looking...
by: Doug Bell | last post by:
Hi I have a datagrid with a combo box, I need to populate the combo with data dependant on the record value. eg for record 1, field Warehouse = 2R so combo would allow selection of locations...
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
by: c_shah | last post by:
I am a beginner and learning VB.net There are two wasy to bind a combobox or a listbox First approach is to assign a datasource and displaymember Second approach is to iterate through datarow...
by: fstenoughsnoopy | last post by:
I have a customer order database and I need to pull a customers information, ie first name, last name, address, city, state, zip, phone, etc, into the oder table. i don't know how to go about...
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.