Изменение типа столбца DataGridView после его заполнения из бд - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте. Есть DataGridView, который заполняется следующим способом:
Листинг программы
  1. ...
  2. CompDataAdapter = new MySqlDataAdapter("SELECT * FROM компании", Connect);
  3. DataSet CompDS = new DataSet();
  4. CompDataAdapter.Fill(CompDS);
  5. CompDataGridView.DataSource = CompDS.Tables[0];
  6. ...
Вопрос в том - как изменить тип некоторых столбцов с того что создается по умолчанию на ComboBoxColumn и присвоить какие то значения свойству Items? Ведь при создании программы столбца как бы нет, он появляется в процессе заполнения DataGridView.

Решение задачи: «Изменение типа столбца DataGridView после его заполнения из бд»

textual
Листинг программы
  1.    foreach (XElement xxx in xe.Element("Frame").Elements())
  2.                 {
  3.  
  4.                     ///
  5.                     /// Common properties
  6.                     ///
  7.  
  8.                     ///
  9.                     /// Appearance
  10.                     ///
  11.                     XElement temp = xxx.Element("Appearance");
  12.                     string name = xxx.Attribute("name").Value.ToString(); ;
  13.                     bool vis = GetValue(temp.Attribute("Visible").Value.ToString());
  14.                     if (!vis) Hiddens++;
  15.                     string HeaderText = temp.Attribute("HeaderText").Value.ToString();
  16.                     string Description = temp.Attribute("Description").Value.ToString();
  17.                     string ToolTip = temp.Attribute("ToolTip").Value.ToString();
  18.                     temp = xxx.Element("Behavior");
  19.                     bool RO = GetValue(temp.Attribute("ReadOnly").Value.ToString());
  20.                     int Length = int.Parse(temp.Attribute("Length").Value);
  21.                     DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
  22.                     dataGridViewCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
  23.                     dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.Window;
  24.                     dataGridViewCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  25.                     dataGridViewCellStyle.ForeColor = System.Drawing.SystemColors.ControlText;
  26.                     dataGridViewCellStyle.SelectionBackColor = System.Drawing.SystemColors.GradientActiveCaption;
  27.                     dataGridViewCellStyle.SelectionForeColor = System.Drawing.SystemColors.InfoText;
  28.                     dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
  29.                    
  30.  
  31.                     ///
  32.                     /// Data
  33.                     ///
  34.                     temp = xxx.Element("Data");
  35.                     string DataPropName = temp.Attribute("DataPropName").Value.ToString();
  36.                     ///
  37.                     /// Columns preparing
  38.                     ///
  39.                     switch (xxx.Element("Project").Attribute("Columntype").Value.ToString())
  40.                     {
  41.                         case "TextBox":
  42.                             DataGridViewTextBoxColumn tb = new DataGridViewTextBoxColumn();
  43.                             tb.Name = name;
  44.                             tb.Visible = vis;
  45.                             if (Visible)
  46.                             {
  47.                                 tb.Resizable = DataGridViewTriState.True;
  48.                                 tb.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  49.                             }
  50.                             tb.HeaderText = HeaderText;
  51.                             tb.ToolTipText = ToolTip;
  52.                             tb.DataPropertyName = DataPropName;
  53.                             tb.ReadOnly = RO;
  54.                             if(Length>0)
  55.                                 tb.MaxInputLength = Length;
  56.                             tb.Tag = 1; // column type
  57.                             //tb.SortMode = DataGridViewColumnSortMode.Programmatic;
  58.                             tb.DefaultCellStyle = dataGridViewCellStyle;
  59.                            
  60.                             this.Columns.Add(tb);
  61.                             break;
  62.                         case "ComboBox":
  63.                             DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
  64.                             cb.Name = name;
  65.                             cb.Visible = vis;
  66.                             if (Visible)
  67.                             {
  68.                                 cb.Resizable = DataGridViewTriState.True;
  69.                                 cb.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  70.                             }
  71.                             cb.HeaderText = HeaderText;
  72.                             cb.ToolTipText = ToolTip;
  73.                             cb.DataPropertyName = DataPropName;
  74.                             cb.DisplayMember = temp.Attribute("DisplayMember").Value.ToString();
  75.                             cb.ValueMember = temp.Attribute("ValueMember").Value.ToString();
  76.                             cb.DataSource = FillDS(temp.Attribute("DataSource").Value.ToString());
  77.                             cb.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
  78.                             cb.Tag = 2;
  79.                             cb.ReadOnly = RO;
  80.                             cb.DefaultCellStyle = dataGridViewCellStyle;
  81.  
  82.                             //cb.SortMode = DataGridViewColumnSortMode.Programmatic;
  83.                             this.Columns.Add(cb);
  84.                             break;

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4.833 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы