locked
Adding User Control in DataGridView RRS feed

  • Question

  • Hi all,

      i want to dispaly Graph in DataGridView control by using a UserControl. Basically this application will display multiple graphs in DataGridView Control. So i just want to add UserContol in a loop at run time.

    Thanks & Regards

    Madan Mohan

    Thursday, March 22, 2007 2:23 PM

Answers

  • So could you please discribe your problem more detail,do you want to add graph in a column or in a row or in several cells?

    I think maybe you should use DataGridViewImageColumn Properties (System.Windows.Forms)

    Friday, March 23, 2007 4:48 AM
  • Hi MadanMohan:

         DataGridView is a column based control you can use its column and cell to display data,

         so I think you can put graphics into cell and how you want those graphics arrange?

         or Datagridview can contain coltrols to display pic such as PictureBox code as follow:

        PictureBox p = new PictureBox();

                p.Image = Image.FromFile("C://logo.gif");

                p.Location = new Point(30, 30);

                this.dataGridView1.Controls.Add(p);

        

    Friday, March 23, 2007 6:46 AM
  • I think you will have to write your own classes inheriting from the DataGridViewColumn and the DataGridViewCell like DataGridViewGraphColumn and DataGridViewGraphCell.
    In MSDN you can read about all overridable members of the classes and what they do.

    Then you will just have to add a DataGridViewGraphColumn to the columns collection of your gridview. It will automatically create new DataGridViewGraphCell objects for each new row you insert.

    Something like that:
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    class Form1 : Form
    {
    [STAThreadAttribute()]
    public static void Main()
    {
    Application.Run(new Form1());
    }

    public Form1()
    {
    DataGridView dataGridView1 = new DataGridView();
    DataGridViewMyColumn col =
    new DataGridViewMyColumn();
    dataGridView1.Columns.Add(col);
    dataGridView1.Rows.Add(new string[] { "" });
    dataGridView1.Rows.Add(new string[] { "" });
    dataGridView1.Rows.Add(new string[] { "" });
    dataGridView1.Rows.Add(new string[] { "" });
    this.Controls.Add(dataGridView1);
    }
    }

    public class DataGridViewMyCell : DataGridViewCell
    {
    protected override void Paint(
    Graphics graphics,
    Rectangle clipBounds,
    Rectangle cellBounds,
    int rowIndex,
    DataGridViewElementStates cellState,
    object value,
    object formattedValue,
    string errorText,
    DataGridViewCellStyle cellStyle,
    DataGridViewAdvancedBorderStyle advancedBorderStyle,
    DataGridViewPaintParts paintParts)
    {
    //do the painting
    }

    ///other overrides

    }

    public class DataGridViewMyColumn : DataGridViewColumn
    {
    public
    DataGridViewMyColumn ()
    {
    this.CellTemplate = new DataGridViewMyCell();
    }
    }
    the code is given for reference only, i have never compiled or tested it
    Friday, March 23, 2007 9:36 AM

All replies

  • So could you please discribe your problem more detail,do you want to add graph in a column or in a row or in several cells?

    I think maybe you should use DataGridViewImageColumn Properties (System.Windows.Forms)

    Friday, March 23, 2007 4:48 AM
  • Hi MadanMohan:

         DataGridView is a column based control you can use its column and cell to display data,

         so I think you can put graphics into cell and how you want those graphics arrange?

         or Datagridview can contain coltrols to display pic such as PictureBox code as follow:

        PictureBox p = new PictureBox();

                p.Image = Image.FromFile("C://logo.gif");

                p.Location = new Point(30, 30);

                this.dataGridView1.Controls.Add(p);

        

    Friday, March 23, 2007 6:46 AM
  • I think you will have to write your own classes inheriting from the DataGridViewColumn and the DataGridViewCell like DataGridViewGraphColumn and DataGridViewGraphCell.
    In MSDN you can read about all overridable members of the classes and what they do.

    Then you will just have to add a DataGridViewGraphColumn to the columns collection of your gridview. It will automatically create new DataGridViewGraphCell objects for each new row you insert.

    Something like that:
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    class Form1 : Form
    {
    [STAThreadAttribute()]
    public static void Main()
    {
    Application.Run(new Form1());
    }

    public Form1()
    {
    DataGridView dataGridView1 = new DataGridView();
    DataGridViewMyColumn col =
    new DataGridViewMyColumn();
    dataGridView1.Columns.Add(col);
    dataGridView1.Rows.Add(new string[] { "" });
    dataGridView1.Rows.Add(new string[] { "" });
    dataGridView1.Rows.Add(new string[] { "" });
    dataGridView1.Rows.Add(new string[] { "" });
    this.Controls.Add(dataGridView1);
    }
    }

    public class DataGridViewMyCell : DataGridViewCell
    {
    protected override void Paint(
    Graphics graphics,
    Rectangle clipBounds,
    Rectangle cellBounds,
    int rowIndex,
    DataGridViewElementStates cellState,
    object value,
    object formattedValue,
    string errorText,
    DataGridViewCellStyle cellStyle,
    DataGridViewAdvancedBorderStyle advancedBorderStyle,
    DataGridViewPaintParts paintParts)
    {
    //do the painting
    }

    ///other overrides

    }

    public class DataGridViewMyColumn : DataGridViewColumn
    {
    public
    DataGridViewMyColumn ()
    {
    this.CellTemplate = new DataGridViewMyCell();
    }
    }
    the code is given for reference only, i have never compiled or tested it
    Friday, March 23, 2007 9:36 AM