Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add row and column at runtime in c# windows based project, like a spreadsheet. I am using user control to add rows but want to know how to add column as well. Please give me suggestion. Thanks.
Posted

It depends on what you are using to display the data.
If you are using a DataGridView, then it will provide all the facilities you will need: MSDN[^]



"Thanks for Reply. But I am using controls which includes textboxes. I am adding at them at run time. so In that case how can I add columns and row. Please reply"

Sorry? Do I understand you right that you are trying to add rows and columns of textboxes? If so, then why? Why make life difficult for yourself?
You can do it - just create them using
C#
TextBox tb = new TextBox();
Set the Location and Size properties, and add them to the forms Controls list:
C#
for (int i = 0; i < 5; i++)
   {
   TextBox tb = new TextBox();
   tb.Size = new Size(100, 25);
   tb.Location = new Point(25, 25 + (i * 30));
   Controls.Add(tb);
   }


But it's a clumsy, inefficient, and hard-to-maintain way to do it.
Why not use a control that is built to do exactly what you need: provide rows and columns of cells...the DataGridView. It will save you a lot of hassle in the long run...
 
Share this answer
 
v2
Comments
lovejaygore 25-Jun-12 14:17pm    
Thanks for Reply. But I am using controls which includes textboxes. I am adding at them at run time. so In that case how can I add columns and row. Please reply.
OriginalGriff 25-Jun-12 14:31pm    
Answer updated.
hi lovejaygore your question was not clear.
by using datagridview we can add columns and rows, hear is the sample code.

C#
private void dataGridView2_RowsAdded(object sender,DataGridViewRowsAddedEventArgs e)
   {

     for (int i = 1; i < e.RowCount; i++)
    this.dataGridView2.Rows[e.RowIndex + i].Cells[0].Value =(e.RowIndex+i).ToString();



    }

i feel happy if it helps...
 
Share this answer
 
Comments
lovejaygore 25-Jun-12 14:17pm    
Thanks for Reply. But I am using controls which includes textboxes. I am adding at them at run time. so In that case how can I add columns and row. Please reply.
sandeep nagabhairava 25-Jun-12 14:24pm    
for what purpose you need to add rows and columns..?
Hope the below code is useful for you!
You can add n number of rows and columns using the method addrowsandcolumns.

Happy coding:)

C#
DataTable dtSource;
private DataTable addrowsandcolumns(int nRow,int ncolumn)
{
    dtSource = new DataTable();
    for (int i = 0; i < ncolumn; i++)
    {
        dtSource.Columns.Add(i.ToString());
    }

    for (int i = 0; i < nRow; i++)
    {
        for (int j = 0; j < ncolumn; j++)
        {
            DataRow dr = dtSource.NewRow();
            dr[j] = "";
            dtSource.Rows.Add(dr);
        }
    }

    return dtSource;
}

private void button1_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = addrowsandcolumns(3, 5);
}
 
Share this answer
 
v2
It's hard to make out what you are trying to do from your question and repetitive replies - somi am guessing.

You have a user control that has text boxes in it. You are adding multiple copies Of the user control to a form, one below the other, to form rows.

If that is correct, then to add columns, you just need to alter the location of the user control on the x axis. Assuming all the user controls have the same width then the left position is c*w where c is the column number and. Is the width
 
Share this answer
 
Add a datagrid to the Winform it definitely looks like a spreadsheet
 
Share this answer
 
Comments
lovejaygore 25-Jun-12 14:17pm    
Thanks for Reply. But I am using controls which includes textboxes. I am adding at them at run time. so In that case how can I add columns and row. Please reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900