Изменение типа столбца DataGridView после его заполнения из бд - C#
Формулировка задачи:
Здравствуйте. Есть DataGridView, который заполняется следующим способом:
Вопрос в том - как изменить тип некоторых столбцов с того что создается по умолчанию на ComboBoxColumn и присвоить какие то значения свойству Items? Ведь при создании программы столбца как бы нет, он появляется в процессе заполнения DataGridView.
...
CompDataAdapter = new MySqlDataAdapter("SELECT * FROM компании", Connect);
DataSet CompDS = new DataSet();
CompDataAdapter.Fill(CompDS);
CompDataGridView.DataSource = CompDS.Tables[0];
...Решение задачи: «Изменение типа столбца DataGridView после его заполнения из бд»
textual
Листинг программы
foreach (XElement xxx in xe.Element("Frame").Elements())
{
///
/// Common properties
///
///
/// Appearance
///
XElement temp = xxx.Element("Appearance");
string name = xxx.Attribute("name").Value.ToString(); ;
bool vis = GetValue(temp.Attribute("Visible").Value.ToString());
if (!vis) Hiddens++;
string HeaderText = temp.Attribute("HeaderText").Value.ToString();
string Description = temp.Attribute("Description").Value.ToString();
string ToolTip = temp.Attribute("ToolTip").Value.ToString();
temp = xxx.Element("Behavior");
bool RO = GetValue(temp.Attribute("ReadOnly").Value.ToString());
int Length = int.Parse(temp.Attribute("Length").Value);
DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
dataGridViewCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
dataGridViewCellStyle.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle.SelectionBackColor = System.Drawing.SystemColors.GradientActiveCaption;
dataGridViewCellStyle.SelectionForeColor = System.Drawing.SystemColors.InfoText;
dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
///
/// Data
///
temp = xxx.Element("Data");
string DataPropName = temp.Attribute("DataPropName").Value.ToString();
///
/// Columns preparing
///
switch (xxx.Element("Project").Attribute("Columntype").Value.ToString())
{
case "TextBox":
DataGridViewTextBoxColumn tb = new DataGridViewTextBoxColumn();
tb.Name = name;
tb.Visible = vis;
if (Visible)
{
tb.Resizable = DataGridViewTriState.True;
tb.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
tb.HeaderText = HeaderText;
tb.ToolTipText = ToolTip;
tb.DataPropertyName = DataPropName;
tb.ReadOnly = RO;
if(Length>0)
tb.MaxInputLength = Length;
tb.Tag = 1; // column type
//tb.SortMode = DataGridViewColumnSortMode.Programmatic;
tb.DefaultCellStyle = dataGridViewCellStyle;
this.Columns.Add(tb);
break;
case "ComboBox":
DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
cb.Name = name;
cb.Visible = vis;
if (Visible)
{
cb.Resizable = DataGridViewTriState.True;
cb.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
cb.HeaderText = HeaderText;
cb.ToolTipText = ToolTip;
cb.DataPropertyName = DataPropName;
cb.DisplayMember = temp.Attribute("DisplayMember").Value.ToString();
cb.ValueMember = temp.Attribute("ValueMember").Value.ToString();
cb.DataSource = FillDS(temp.Attribute("DataSource").Value.ToString());
cb.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
cb.Tag = 2;
cb.ReadOnly = RO;
cb.DefaultCellStyle = dataGridViewCellStyle;
//cb.SortMode = DataGridViewColumnSortMode.Programmatic;
this.Columns.Add(cb);
break;