473,396 Members | 1,755 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,396 software developers and data experts.

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

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
0 1250

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

Similar topics

3
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...
3
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"; ...
7
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...
6
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...
30
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...
6
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...
4
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...
2
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.