Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Runtime row
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 24-Jun-12 0:28am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
TextBox tb = new TextBox();
Set the Location and Size properties, and add them to the forms Controls list:
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...
  Permalink  
v2
Comments
lovejaygore at 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 at 25-Jun-12 14:31pm
   
Answer updated.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hi lovejaygore your question was not clear.
by using datagridview we can add columns and rows, hear is the sample code.
 
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...
  Permalink  
Comments
lovejaygore at 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 at 25-Jun-12 14:24pm
   
for what purpose you need to add rows and columns..?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hope the below code is useful for you!
You can add n number of rows and columns using the method addrowsandcolumns.
 
Happy codingSmile | :)
 
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);
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Add a datagrid to the Winform it definitely looks like a spreadsheet
  Permalink  
Comments
lovejaygore at 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)

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 25 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100