Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am pretty new to windows programming.
I have a DataGridView (DGV) with one of the column of type DataGridViewComboBox(DGVC).
I have binded the DGV toa dataset and binded the DGVC with the dataTable. One the based of one of the column value dataset I am settign the selection in DGVC at the time of loading. For this I am using CellFormatting event.

Concern:
After the grid is laoded perfectly, and I change the value of the DGVC to some new value, the value is not retained the moment I move to some other cell of the DGV.

Please see my code.

const string GFRN_TYPE_COMBO_COL_NAME = "GFRNTypeComboColumn";

public frmGFRNDetails()
   {
       InitializeComponent();
   }

   private void frmGFRNDetails_Load(object sender, EventArgs e)
   {
       DataSet ds = GetDummyData();
       dgGFRNDetails.DataSource = ds.Tables[0];

       BindGFRNTypeComboColumn();

       dgGFRNDetails.Columns[dgGFRNDetails.ColumnCount - 1].DisplayIndex = 2;

       dgGFRNDetails.Columns["GFRN"].HeaderText = "GFRN";
       dgGFRNDetails.Columns["GFRN_TYPE"].HeaderText = "GFRN Type";
       dgGFRNDetails.Columns["GFRN_TYPE"].Visible = false;
       dgGFRNDetails.Columns["REGULATORYSEGMENT"].HeaderText = "Regulatory Segment";
       dgGFRNDetails.Columns["SUPPORT"].HeaderText = "Support";
       dgGFRNDetails.Columns["GFCID"].HeaderText = "GFCID";
       dgGFRNDetails.Columns["COMPANYNAME"].HeaderText = "Company Name";
       dgGFRNDetails.Columns["FRR"].HeaderText = "FRR";
       dgGFRNDetails.Columns["FRR_SOURCE"].HeaderText = "FRR SOURCE";
       dgGFRNDetails.Columns["OSUC"].HeaderText = "OSCU";
       dgGFRNDetails.Columns["OSUC_SOURCE"].HeaderText = "OSUC SOURCE";
   }

   private void btnClose_Click(object sender, EventArgs e)
   {
       this.Hide();
   }

   private void BindGFRNTypeComboColumn()
   {
       DataTable dt = new DataTable();
       dt.Columns.Add("Item");
       dt.Columns.Add("Value");
       dt.Rows.Add(" ", "0");
       dt.Rows.Add("Recourse", "1");
       dt.Rows.Add("Non Recourse", "2");
       DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
       cmb.Name = GFRN_TYPE_COMBO_COL_NAME;
       cmb.HeaderText = "GFRN Type";
       cmb.DisplayMember = "Item";
       cmb.ValueMember = "Value";
       dgGFRNDetails.Columns.Add(cmb);
       cmb.DataSource = dt;
       for (int i = 0; i < dgGFRNDetails.Rows.Count; i++)
       {
           dgGFRNDetails.Rows[i].Cells[GFRN_TYPE_COMBO_COL_NAME].Value = (cmb.Items[0] as DataRowView).Row[1].ToString();
       }
   }

   private DataSet GetDummyData()
   {
       DataSet dsGFRNDetails = new DataSet();
       dsGFRNDetails = new localhost.Service().GetFRRDetails("1000981067");
       return dsGFRNDetails;
   }

   private void dgGFRNDetails_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
   {


   }

   private void dgGFRNDetails_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
   {
       var gfrnValue = dgGFRNDetails.Rows[e.RowIndex].Cells[2].Value;
       string gfrn = string.Empty;
       if (gfrnValue != null)
       {
           gfrn = gfrnValue.ToString();
       }
       if (!string.IsNullOrEmpty(gfrn))
       {
           DataGridViewComboBoxColumn comboCol = this.dgGFRNDetails.Columns[GFRN_TYPE_COMBO_COL_NAME] as DataGridViewComboBoxColumn;
           if (comboCol != null)
           {
               switch (gfrn)
               {
                   case "Recourse":
                       dgGFRNDetails.Rows[e.RowIndex].Cells[GFRN_TYPE_COMBO_COL_NAME].Value = (comboCol.Items[1] as DataRowView).Row[1].ToString();
                       break;
                   case "Non Recourse":
                       dgGFRNDetails.Rows[e.RowIndex].Cells[GFRN_TYPE_COMBO_COL_NAME].Value = (comboCol.Items[2] as DataRowView).Row[1].ToString();
                       break;
                   default:
                       dgGFRNDetails.Rows[e.RowIndex].Cells[GFRN_TYPE_COMBO_COL_NAME].Value = (comboCol.Items[0] as DataRowView).Row[1].ToString();
                       break;
               }
           }
           // e.CellStyle.BackColor = Color.Pink;
       }
   }



Please HELP !!

"Happy Coding"
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900