CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2005
    Posts
    114

    Problem with DataGrid Control..

    Hi gurus,

    I am new to C#.. The problem i am facing here is 1)i tried to place a combo boxes inside a DataGridControl, but it is not working.. 2) I dont know how to color the particular Column cells.. Please send me some ideas or sample of doing that.. Any suggestions are welcome

    Thanks in advance

    Regards

  2. #2
    Join Date
    May 2002
    Location
    Ukraine
    Posts
    228

    Re: Problem with DataGrid Control..

    Common idea using ColumnStyle

    public class ComboBoxGridColumn : DataGridColumnStyle

    styles assigned in AddGridStyle()

    I made such example:

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace DataGrid2
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {

    private DataTable namesDataTable;
    private System.Windows.Forms.DataGrid grid;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    namesDataTable = new DataTable("NamesTable");
    namesDataTable.Columns.Add(new DataColumn("Name"));

    DataColumn cmbColumn = new DataColumn
    // ("ComboStr", typeof(DateTime));
    ("ComboStr", typeof(string));
    namesDataTable.Columns.Add(cmbColumn);

    DataSet namesDataSet = new DataSet();
    namesDataSet.Tables.Add(namesDataTable);
    grid.DataSource = namesDataSet;
    grid.DataMember = "NamesTable";
    AddGridStyle();
    AddData();

    grid.ReadOnly = true;
    }

    private void AddGridStyle()
    {
    DataGridTableStyle myGridStyle = new DataGridTableStyle();
    myGridStyle.MappingName = "NamesTable";

    DataGridTextBoxColumn nameColumnStyle =
    new DataGridTextBoxColumn();
    nameColumnStyle.MappingName = "Name";
    nameColumnStyle.HeaderText= "Name";
    myGridStyle.GridColumnStyles.Add(nameColumnStyle);

    ComboBoxGridColumn cmbColumnStyle =
    new ComboBoxGridColumn();
    cmbColumnStyle.MappingName = "ComboStr";
    cmbColumnStyle.HeaderText = "ComboStr";
    cmbColumnStyle.Width = 100;
    myGridStyle.GridColumnStyles.Add(cmbColumnStyle);

    cmbColumnStyle.AddString("Str1");
    cmbColumnStyle.AddString("Str2");
    cmbColumnStyle.AddString("Str3");
    cmbColumnStyle.AddString("Str4");

    grid.TableStyles.Add(myGridStyle);
    }

    private void AddData()
    {

    DataRow dRow = namesDataTable.NewRow();
    dRow["Name"] = "Name 1";
    // dRow["ComboStr"] = new DateTime(2001, 12, 01);
    dRow["ComboStr"] = "Str1";
    namesDataTable.Rows.Add(dRow);

    dRow = namesDataTable.NewRow();
    dRow["Name"] = "Name 2";
    // dRow["ComboStr"] = new DateTime(2001, 12, 04);
    dRow["ComboStr"] = "Str2";
    namesDataTable.Rows.Add(dRow);

    dRow = namesDataTable.NewRow();
    /* dRow["Name"] = "Name 3";
    dRow["ComboStr"] = new DateTime(2001, 12, 29);
    namesDataTable.Rows.Add(dRow);

    dRow = namesDataTable.NewRow();
    dRow["Name"] = "Name 4";
    dRow["ComboStr"] = new DateTime(2001, 12, 13);
    namesDataTable.Rows.Add(dRow);

    dRow = namesDataTable.NewRow();
    dRow["Name"] = "Name 5";
    dRow["ComboStr"] = new DateTime(2001, 12, 21);
    namesDataTable.Rows.Add(dRow);
    */
    namesDataTable.AcceptChanges();
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.grid = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit();
    this.SuspendLayout();
    //
    // grid
    //
    this.grid.DataMember = "";
    this.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.grid.Location = new System.Drawing.Point(16, 28);
    this.grid.Name = "grid";
    this.grid.Size = new System.Drawing.Size(244, 212);
    this.grid.TabIndex = 0;
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.grid);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    ((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit();
    this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {

    }

    public class ComboBoxGridColumn : DataGridColumnStyle
    {
    private ComboBox myComboBox = new ComboBox();
    private bool isEditing;

    public ComboBoxGridColumn() : base()
    {
    myComboBox.Visible = false;

    }

    public void AddString(string sStr)
    {
    myComboBox.Items.Add(sStr);
    }


    protected override void Abort(int rowNum)
    {
    isEditing = false;
    myComboBox.SelectedIndexChanged -=
    new EventHandler(ComboBoxSelChanged);
    Invalidate();
    }

    protected override bool Commit
    (CurrencyManager dataSource, int rowNum)
    {
    myComboBox.Bounds = Rectangle.Empty;

    myComboBox.SelectedIndexChanged -=
    new EventHandler(ComboBoxSelChanged);

    if (!isEditing)
    return true;

    isEditing = false;

    try
    {
    string value = myComboBox.Text;
    SetColumnValueAtRow(dataSource, rowNum, value);
    }
    catch (Exception)
    {
    Abort(rowNum);
    return false;
    }

    Invalidate();
    return true;
    }

    protected override void Edit(
    CurrencyManager source,
    int rowNum,
    Rectangle bounds,
    bool readOnly,
    string instantText,
    bool cellIsVisible)
    {

    object value = GetColumnValueAtRow(source, rowNum);

    if (cellIsVisible)
    {
    myComboBox.Bounds = new Rectangle
    (bounds.X + 2, bounds.Y + 2,
    bounds.Width - 4, bounds.Height - 4);
    myComboBox.Text = (string)value;
    myComboBox.Visible = true;
    myComboBox.SelectedIndexChanged +=
    new EventHandler(ComboBoxSelChanged);
    }
    else
    {
    myComboBox.Text = (string)value;
    myComboBox.Visible = false;
    }

    if (myComboBox.Visible)
    DataGridTableStyle.DataGrid.Invalidate(bounds);

    }

    protected override Size GetPreferredSize(
    Graphics g,
    object value)
    {
    return new Size(100, myComboBox.PreferredHeight + 4);
    }

    protected override int GetMinimumHeight()
    {
    return myComboBox.PreferredHeight + 4;
    }

    protected override int GetPreferredHeight(Graphics g,
    object value)
    {
    return myComboBox.PreferredHeight + 4;
    }

    protected override void Paint(Graphics g,
    Rectangle bounds,
    CurrencyManager source,
    int rowNum)
    {
    Paint(g, bounds, source, rowNum, false);
    }
    protected override void Paint(
    Graphics g,
    Rectangle bounds,
    CurrencyManager source,
    int rowNum,
    bool alignToRight)
    {
    Paint(
    g,bounds,
    source,
    rowNum,
    Brushes.Red,
    Brushes.Blue,
    alignToRight);
    }
    protected override void Paint(
    Graphics g,
    Rectangle bounds,
    CurrencyManager source,
    int rowNum,
    Brush backBrush,
    Brush foreBrush,
    bool alignToRight)
    {
    string sStr = (string)
    GetColumnValueAtRow(source, rowNum);
    Rectangle rect = bounds;
    g.FillRectangle(backBrush,rect);
    rect.Offset(0, 2);
    rect.Height -= 2;
    g.DrawString(sStr,
    this.DataGridTableStyle.DataGrid.Font,
    foreBrush, rect);
    }

    protected override void SetDataGridInColumn(DataGrid value)
    {
    base.SetDataGridInColumn(value);
    if (myComboBox.Parent != null)
    {
    myComboBox.Parent.Controls.Remove
    (myComboBox);
    }
    if (value != null)
    {
    value.Controls.Add(myComboBox);
    }
    }

    private void ComboBoxSelChanged(object sender, EventArgs e)
    {
    this.isEditing = true;
    base.ColumnStartedEditing(myComboBox);
    }
    }

    }
    }

  3. #3
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

  4. #4
    Join Date
    Jul 2002
    Location
    India
    Posts
    505

    Re: Problem with DataGrid Control..

    Answers to these and other datagrid questions...

    http://www.syncfusion.com/faq/windowsforms/Default.aspx

  5. #5
    Join Date
    Sep 2005
    Posts
    114

    Re: Problem with DataGrid Control..

    Thanks for replying me and shared for ur ideas with me.. Now its working with fine and i am facing new problemm..

    1 ) what i want is i want to name a row of the datagrid.. So far i named only columns using datacolumns.. Is it possible to name a row?

    2) And other thing is i want to rearrnage rows and columns of a datagrid.. That means that i want to display the datagrid as in the form of graph datas..

    ie. 1 2 3 4

    A 10 20 30 40
    B 0.4 0.5 0.6 0.7

    like this.. Is it possible?

    Send me or share me ur ideas with me..
    Thanks in advance

    Regards,
    SAravanan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured