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

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

P: 81
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
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.