473,799 Members | 3,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

77 New Member
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 1285

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

Similar topics

3
4989
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 and 90. The default is set at 30. Question1: When the form opens, there are no records displayed although there are many records that fit the criteria of 30. If I put a button on the form to do a requery and press the button, all the records...
3
12107
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"; cb1.ValueMember = cb1.DisplayMember;
7
23530
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 for a match between cVal and the selectedValue then stop when I do have a match the obvious problem is that each iteration fires the selectedIndexChanged but also for a large list this will slow everything down. There must be a better way int...
6
1788
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 valid for 2R record 2 has Warehouse = 2M so combo on that row has a selection of locations valid for 2M I can set the combo datasource but can't see how to set it on a row by row basis.
30
4606
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 corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
6
2883
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 and the coresponding 'Id' from the lookup table is to be inserted into the field of the new record. I have two simple tables. "tblPerson" is the data table. The lookup
4
35321
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 collection This works With ComboPaymentType .DataSource = dsComboItems.Tables("PaymentType") .DisplayMember = "PaymentType"
2
946
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 making this work...
2
3322
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 CurrentChanged event of the BindingSource of the combobox to update the textbox. When selecting an item in the combobox or when selecting a row in the grid, it is updating the textbox correctly. The problem is when I apply a filter in the grid, and then...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9067
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.