Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to Add the Rows and columns in DataGridView on vertical and horizontal scroll of DataGridView
 

How to Add the Rows and columns in DataGridView on vertical and horizontal scroll of DataGridView

Hi all,

I have one problem how to add the rows and columns when i scroll the DataGridView vertically and horizontally.when i scroll vertically new rows should be add and when i scroll the horizontally new columns should be added with new header text(day name).The DataGridView will look like an excel sheet.is this method is correct or is there any other method?.

Please clarify my doughts.

Thanks a lot

Regards
Sanjay.
sanjaypakale  Friday, August 07, 2009 5:39 AM

Hi sanjaypakale,

Yes, you are right. We can handle the Scroll event of the DataGridView to add columns or rows. We need to pay attentions to the items below:

1. Check the count of the columns/rows. We need to restrict the count of the rows/columns so that the adding process can end when we approach to a maximum value. Otherwise, the adding process cannot be stopped and would cause a freeze of the program.

2. When we add a row, we have different ways according to whether the DataGridView is bound or not. If the DataGridView is unbound, we can directly call the Add method of the Rows property of the DataGridView. If it is bound, we need to get the underlying data source and add a row via that object. For example, if the data source if of type DataTable, we can follow the code below to add a row:

DataTable dt = this.dataGridView1.DataSource as DataTable;

dt.Rows.Add(dt.NewRow());

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 10, 2009 7:33 AM
Hi sanjaypakale,

This is a code snippet:
public partial class MyDGVForm : Form
{
    public MyDGVForm()
    {
        InitializeComponent();
    }

    //Max count of columns.
    const int MAX_COLUMN_COUNT = 10;
    //Max count of rows.
    const int MAX_ROW_COUNT = 100;
    private void MyDGVForm_Load(object sender, EventArgs e)
    {
        this.dataGridView1.DataSource = MyLib.Builder.GetDateTable();
        //Handle the scroll event.
        this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
    }       

    void dataGridView1_Scroll(object sender, ScrollEventArgs e)
    {
        if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
        {
            if (this.dataGridView1.Columns.Count < 10)
            {
                //Add a column.
                this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
            }
        }
        else
        {
            if (this.dataGridView1.Rows.Count < 150)
            {
                //Add a row if bound.
                DataTable dt = this.dataGridView1.DataSource as DataTable;
                dt.Rows.Add(dt.NewRow());

                //Add a row if unbound.
                //this.dataGridView1.Rows.Add();
            }
        }
    }
}


Regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 10, 2009 7:43 AM

Hi sanjaypakale,

Yes, you are right. We can handle the Scroll event of the DataGridView to add columns or rows. We need to pay attentions to the items below:

1. Check the count of the columns/rows. We need to restrict the count of the rows/columns so that the adding process can end when we approach to a maximum value. Otherwise, the adding process cannot be stopped and would cause a freeze of the program.

2. When we add a row, we have different ways according to whether the DataGridView is bound or not. If the DataGridView is unbound, we can directly call the Add method of the Rows property of the DataGridView. If it is bound, we need to get the underlying data source and add a row via that object. For example, if the data source if of type DataTable, we can follow the code below to add a row:

DataTable dt = this.dataGridView1.DataSource as DataTable;

dt.Rows.Add(dt.NewRow());

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 10, 2009 7:33 AM
Thanks Aland,

can u send me some 1 sample example?
thanks

sanjay.
sanjaypakale  Monday, August 10, 2009 7:37 AM
Hi sanjaypakale,

This is a code snippet:
public partial class MyDGVForm : Form
{
    public MyDGVForm()
    {
        InitializeComponent();
    }

    //Max count of columns.
    const int MAX_COLUMN_COUNT = 10;
    //Max count of rows.
    const int MAX_ROW_COUNT = 100;
    private void MyDGVForm_Load(object sender, EventArgs e)
    {
        this.dataGridView1.DataSource = MyLib.Builder.GetDateTable();
        //Handle the scroll event.
        this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
    }       

    void dataGridView1_Scroll(object sender, ScrollEventArgs e)
    {
        if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
        {
            if (this.dataGridView1.Columns.Count < 10)
            {
                //Add a column.
                this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
            }
        }
        else
        {
            if (this.dataGridView1.Rows.Count < 150)
            {
                //Add a row if bound.
                DataTable dt = this.dataGridView1.DataSource as DataTable;
                dt.Rows.Add(dt.NewRow());

                //Add a row if unbound.
                //this.dataGridView1.Rows.Add();
            }
        }
    }
}


Regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 10, 2009 7:43 AM

You can use google to search for other answers

Custom Search

More Threads

• inserting excel data into sql server through vb.net
• Sorting databound ComboBox
• datagridview currentrowindex and select & Unselect
• [C#]datagridview copy/paste entire rows
• Dataview filter on Double data type fails
• Help with strange error message - dataGrid
• ContextMenuStrip Event Handler help.
• Clear or hide specific cell in datagrid view
• StackOverflowException assigning a bitmap to a cell value in DataGridView_CellFormatting
• 2 DataGridView Questions