Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a windows Application.
I am using Datagridview.
In the Datagridview i am haivng three DataGridViewComboBoxColumn and one DataGridViewTextBoxColumn.
I have bound the comboboxes and svaing the values.
I am saving Id in the database.
When i fetch and display in the combobox, for first combo alone the id is displayed rather than value.


private void CustomConfiguration()
       {
           DataGridViewComboBoxCell dcbProgramName;
           DataGridViewComboBoxCell dcbBatchMode;
           DataGridViewComboBoxCell dcbBatchValue;
           DataGridViewTextBoxCell dcbUserCode;
           dtCustomCFG = objWorkSourceUtility.GetCustomConfiguration();
           DataTable dtProgram = objWorkSourceUtility.GetProgramNames();
           DataTable dtBatchMode = objWorkSourceUtility.GetBatchModes();
           DataTable dtBatchValue = objWorkSourceUtility.GetBatchValue();
           dgCustomCFG.AutoGenerateColumns = false;
           dtCustomCFGEdit.Clear();
           dtCustomCFGEdit.Columns.Clear();
           dtCustomCFGEdit.Columns.Add("ProgramID", typeof(Int32));
           dtCustomCFGEdit.Columns.Add("BatchValue", typeof(Int32));
           dtCustomCFGEdit.Columns.Add("BatchMode", typeof(Int32));
           dtCustomCFGEdit.Columns.Add("UserCodes", typeof(String));
           for (int i = 0; i < dtCustomCFG.Rows.Count; i++)
           {
               dtCustomCFGEdit.Rows.Add(dtCustomCFG.Rows[i][0].ToString(), dtCustomCFG.Rows[i][1].ToString(), dtCustomCFG.Rows[i][2].ToString(), dtCustomCFG.Rows[i][3].ToString());
               dtCustomCFGEdit.NewRow();
           }
           if (LoadCustomConfiguration)
           {
               if (dgCustomCFG.Rows.Count == 0)
               {
                   DataGridViewTextBoxColumn UserCode = new DataGridViewTextBoxColumn();
                   UserCode.HeaderText = "User Codes";
                   UserCode.DataPropertyName = "UserCodes";
                   DataGridViewComboBoxColumn cmbBatchModes = new DataGridViewComboBoxColumn();
                   cmbBatchModes.HeaderText = "Batch Modes";
                   cmbBatchModes.DataSource = dtBatchMode;
                   cmbBatchModes.DisplayMember = "BatchModeDesc";
                   cmbBatchModes.ValueMember = "BatchMode";
                   cmbBatchModes.DataPropertyName = "BatchMode";
                   //cmbBatchModes.ValueType = typeof(int);
                   DataGridViewComboBoxColumn cmbProgramName = new DataGridViewComboBoxColumn();
                   cmbProgramName.HeaderText = "Program Name";
                   cmbProgramName.DataSource = dtProgram;
                   cmbProgramName.DisplayMember = "ProgramIDDesc";
                   cmbProgramName.ValueMember = "ProgramID";
                   cmbProgramName.DataPropertyName = "ProgramID";
                   //cmbProgramName.ValueType = typeof(int);

                   DataGridViewComboBoxColumn cmbBatchValues = new DataGridViewComboBoxColumn();
                   cmbBatchValues.HeaderText = "Batch Values";
                   cmbBatchValues.DataSource = dtBatchValue;
                   cmbBatchValues.DisplayMember = "BatchValueDesc";
                   cmbBatchValues.ValueMember = "BatchValue";
                   cmbBatchValues.DataPropertyName = "BatchValue";
                   //cmbBatchValues.ValueType = typeof(int);
                   dgCustomCFG.DataSource = dtCustomCFGEdit;
                   dgCustomCFG.Columns.AddRange(cmbProgramName, cmbBatchModes, cmbBatchValues, UserCode);

                   for (int i = 0; i < dgCustomCFG.Rows.Count - 1; i++)
                   {
                       dgCustomCFG.Rows[i].Cells[0].Value = (dtCustomCFGEdit.Rows[i][0].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][0].ToString().Trim());
                       dgCustomCFG.Rows[i].Cells[1].Value = (dtCustomCFGEdit.Rows[i][1].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][1].ToString().Trim());
                       dgCustomCFG.Rows[i].Cells[2].Value = (dtCustomCFGEdit.Rows[i][2].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][2].ToString().Trim());
                   }
               }
           }
           else
           {
               for (int i = 0; i < dgCustomCFG.Rows.Count; i++)
               {
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[0]).DataSource = dtProgram;
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[0]).ValueMember = "ProgramID";
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[0]).DisplayMember = "ProgramIDDesc";
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[0]).DataPropertyName = "ProgramID";
                   //((DataGridViewComboBoxColumn)dgCustomCFG.Columns[0]).ValueType = typeof(int);
                   //dgCustomCFG.Rows[i].Cells[0].Value = 0;
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[1]).DataSource = dtBatchMode;
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[1]).ValueMember = "BatchMode";
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[1]).DisplayMember = "BatchModeDesc";
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[1]).DataPropertyName = "BatchMode";
                   //((DataGridViewComboBoxColumn)dgCustomCFG.Columns[1]).ValueType = typeof(int);

                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[2]).DataSource = dtBatchValue;
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[2]).ValueMember = "BatchValue";
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[2]).DisplayMember = "BatchValueDesc";
                   ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[2]).DataPropertyName = "BatchValue";
                   //((DataGridViewComboBoxColumn)dgCustomCFG.Columns[2]).ValueType = typeof(int);

               }
               dgCustomCFG.DataSource = dtCustomCFGEdit;
           }

           for (int i = 0; i < dgCustomCFG.Rows.Count - 1; i++)
           {
               if (!string.IsNullOrEmpty(dtCustomCFGEdit.Rows[i][0].ToString()))
               {
                   if (dgCustomCFG.Rows[i].Cells[3].Value.ToString() == dtCustomCFGEdit.Rows[i][3].ToString())
                   {
                       dcbProgramName = (DataGridViewComboBoxCell)dgCustomCFG.Rows[i].Cells[0];
                       dcbProgramName.Value = (dtCustomCFGEdit.Rows[i][0].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][0].ToString().Trim());
                       dcbBatchMode = (DataGridViewComboBoxCell)dgCustomCFG.Rows[i].Cells[1];
                       dcbBatchMode.Value = (dtCustomCFGEdit.Rows[i][1].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][1].ToString().Trim());
                       dcbBatchValue = (DataGridViewComboBoxCell)dgCustomCFG.Rows[i].Cells[2];
                       dcbBatchValue.Value = (dtCustomCFGEdit.Rows[i][2].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][2].ToString().Trim());
                       dcbUserCode = (DataGridViewTextBoxCell)dgCustomCFG.Rows[i].Cells[3];
                       dcbUserCode.Value = dtCustomCFGEdit.Rows[i][3].ToString().Trim();
                   }
               }
               else
               {
                   dgCustomCFG.Rows[i].Cells[0].Value = (dtCustomCFGEdit.Rows[i][0].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][0].ToString().Trim());
                   dgCustomCFG.Rows[i].Cells[1].Value = (dtCustomCFGEdit.Rows[i][1].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][1].ToString().Trim());
                   dgCustomCFG.Rows[i].Cells[2].Value = (dtCustomCFGEdit.Rows[i][2].ToString().Trim() == "") ? 0 : Convert.ToInt32(dtCustomCFGEdit.Rows[i][2].ToString().Trim());
               }
               ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[0]).Width = 175;
               ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[1]).Width = 175;
               ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[2]).Width = 175;
               ((DataGridViewTextBoxColumn)dgCustomCFG.Columns[3]).Width = 225;
               ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[0]).SortMode = DataGridViewColumnSortMode.NotSortable;
               ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[1]).SortMode = DataGridViewColumnSortMode.NotSortable;
               ((DataGridViewComboBoxColumn)dgCustomCFG.Columns[2]).SortMode = DataGridViewColumnSortMode.NotSortable;
               ((DataGridViewTextBoxColumn)dgCustomCFG.Columns[3]).SortMode = DataGridViewColumnSortMode.NotSortable;

           }

       }
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