473,406 Members | 2,843 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,406 software developers and data experts.

Rebinding DGV Combobox Column. C#.Net 2008, Framework 3.5.

88 64KB
I have a dgv that's bound to a table tblQuoteItems.
One of the columns of the dgv is ItemID which is a combobox column.
Its value is obtained from ItemID field of tblQuoteItems.
Before binding the dgv, the ItemID column is populated from
tblItems table containing fields ItemID and Item.
The ItemID column of the dgv has ItemID as valuemember and Item as
displaymember.
Now is it possible to rebind only the ItemID column while keeping the
dgv state intact i.e. without rebinding the dgv?
For example I open up a lookup form and modify tblItems table,
then I want the effect to be acted upon the ItemID column of the dgv
without affecting the state of the dgv.
I recalled BindDGVComboBox method defined underneath after modifying
the values of tblItems but of no avail. Please help.

My code follows:

Expand|Select|Wrap|Line Numbers
  1. SqlConnection con;
  2. string strSql;
  3. SqlDataAdapter da;
  4. DataTable dt;
  5. SqlCommandBuilder cb;
  6. BindingSource bs = null;
  7.  
  8. void PopulateDgv()
  9. {
  10.     LineID = new DataGridViewTextBoxColumn();
  11.     QuoteID = new DataGridViewTextBoxColumn();
  12.     ItemID = new DataGridViewComboBoxColumn();
  13.     LookupItems = new DataGridViewButtonColumn();
  14.     Quantity = new DataGridViewTextBoxColumn();
  15.     UnitPrice = new DataGridViewTextBoxColumn();
  16.     LineTotal = new DataGridViewTextBoxColumn();
  17.  
  18.     dgvItems.Columns.Clear();
  19.     dgvItems.DataSource = null;
  20.     dgvItems.Columns.AddRange(new DataGridViewColumn[] {LineID, QuoteID, ItemID, LookupItems, Quantity, UnitPrice, LineTotal });
  21.     LineID.Name = "LineID";
  22.     LineID.DataPropertyName = "LineID";
  23.     LineID.Visible = false;
  24.     QuoteID.Name = "QuoteID";
  25.     QuoteID.DataPropertyName = "QuoteID";
  26.     QuoteID.Visible = false;
  27.     ItemID.Name = "ItemID";
  28.     ItemID.DataPropertyName = "ItemID";
  29.     LookupItems.Name = "LookupItems";
  30.     LookupItems.DataPropertyName = "LookupItems";
  31.     LookupItems.DefaultCellStyle.NullValue = "...";
  32.     LookupItems.Resizable = DataGridViewTriState.False;
  33.     Quantity.Name = "Quantity";
  34.     Quantity.DataPropertyName = "Quantity";
  35.     UnitPrice.Name = "UnitPrice";
  36.     UnitPrice.DataPropertyName = "UnitPrice";
  37.     LineTotal.Name = "LineTotal";
  38.     LineTotal.DataPropertyName = "LineTotal";
  39.     LineTotal.ReadOnly = true;
  40.  
  41.     BindDGVComboBox(ItemID, "Select ItemID,Item From tblShopItems Order By ItemID", "ItemID", "Item");
  42.  
  43.     if (bs == null) { bs = new BindingSource(); }
  44.     if (dt == null) { dt = new DataTable(); }
  45.     strSql = "Select LineID,QuoteID,ItemID,'...' As LookupItems,Quantity,UnitPrice,LineTotal From tblShopQuotesItems Where QuoteID=" + Globals.Val(Tag);
  46.     da = CreateDataAdapter(strSql);
  47.     cb = new SqlCommandBuilder(da);
  48.     dt.Locale = System.Globalization.CultureInfo.InvariantCulture;
  49.     dt.Clear();
  50.     da.Fill(dt);
  51.     bs.DataSource = dt;
  52.     dgvItems.DataSource = bs;
  53. }
  54.  
  55. void BindDGVComboBox(DataGridViewComboBoxColumn objComboBox, string strQuery, string strValueMember, string strDisplayMember)
  56. {
  57.     DataSet ds = new DataSet();
  58.  
  59.     ds = CreateDataSet(strQuery);
  60.     DataView dv = ds.Tables["dtDataTable"].DefaultView;
  61.     objComboBox.DataSource = null;
  62.     objComboBox.DataSource = dv;
  63.     if (strValueMember != null) { objComboBox.ValueMember = strValueMember; }
  64.     objComboBox.DisplayMember = strDisplayMember;
  65. }
  66.  
  67. DataSet CreateDataSet(string strQuery)
  68. {
  69.     DataSet ds = new DataSet();
  70.     SqlDataAdapter da = CreateDataAdapter(strQuery);
  71.     SqlCommandBuilder cb = new SqlCommandBuilder(da);
  72.     da.Fill(ds, "dtDataTable");
  73.     return ds;
  74. }
  75.  
  76. SqlDataAdapter CreateDataAdapter(string strQuery)
  77. {
  78.     SetConnection();
  79.     SqlDataAdapter daAdapter = new SqlDataAdapter(strQuery, con);
  80.     CloseConnection();
  81.     return daAdapter;
  82. }
  83.  
  84. void SetConnection()
  85. {
  86.     if (con == null) { con = new SqlConnection(GetConnectionString()); }
  87.     if (con.State == ConnectionState.Closed) { con.Open(); }
  88. }
  89.  
  90. string GetConnectionString()
  91. {
  92.     string strConn;
  93.     strConn = "Persist Security Info=False;User ID=sa;Password=;Initial Catalog=TestDB;Data Source=MyServer";
  94.     return strConn;
  95. }
  96.  
Aug 17 '10 #1
0 1205

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

Similar topics

5
by: Lynn C | last post by:
I have a combobox column in a datagrid. (I am using the example from Geoge Shepard's website on deriving a custom column style from DataGridTextBoxColumn). The problem I am having is when you make a...
0
by: Sergiu Cojocaru | last post by:
I have a combobox column in datagrid, a column derived from datagridtextboxcolumn, with overriden methods commit, edit, leavecombo for combobox, declared as a variabile -- standart aproach to this...
0
by: AR | last post by:
I have a datagrid containing a single combobox column. It also contains an 'Add New Row' button at the bottom of the datagrid. The datagrid is in a form that contains 'Save' and 'Restore' buttons....
2
by: paaal | last post by:
hi! How can I create a combobox column in a datagrid? I downloaded an example where I copied a reference and added it to my own project. It worked out great, but it's only a trial of a...
4
by: Matt | last post by:
I have been searching all over the web for a way to sort a DataGridView based on the actual text being shown in a ComboBox column as opposed to the underlying value (an ID in this case). Can anyone...
3
by: sklett | last post by:
I'm changing from a DataGrid to a DataGridView and have run across a problem. The items that are bound to the DataGrid have an int Property that represents a primary key of a lookup table in my...
2
by: David Jackson | last post by:
Hello, I have an unbound DataGridView of which one of the columns is a ComboBox colum containing category data, plus an additional option called <newSo when the ComboBox is dropped down it looks...
0
by: marisenthil | last post by:
i am created windows application. i have a gridview in form1 with combobox column. using binding source binded data from the database to gridview except combobox column. i used separate binding...
3
by: Motawee | last post by:
i am making a windows form application that contains a datagridview that is filled with employees names the program is for the attendance absence holidays of employees in the company so i...
0
by: priyamtheone | last post by:
There's an editable datagridview populated from a table say tblItems. Among the columns of the datagridview there's a combobox column named 'Category'. This column is populated by the respective...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.