Answer to Question #248071 in C# for cooki

Question #248071

Hello,

In datagridview, how do I create rows and columns programmatically? I'm trying to create a Spreadsheet.

column a - z

row 1 - 50

Thank you.


1
Expert's answer
2021-10-07T14:00:08-0400
  private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = true;
            DataTable dt = new DataTable(); 
            DataRow dr = null;
            char letter='a';
            while(letter<='z'){
                dt.Columns.Add(new DataColumn(letter.ToString(), typeof(string)));
                letter++;
            }
            for (int i = 1; i <= 50; i++) {
                dr = dt.NewRow();
                dt.Rows.Add(dr);
            }
            


            //Bind the Gridview   
            dataGridView1.DataSource = dt;
            for (int i = 1; i <= 50; i++)
            {
                this.dataGridView1.Rows[i - 1].HeaderCell.Value = (i).ToString();
            }
        }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS