0% found this document useful (0 votes)
82 views1 page

Dynamic GridView in C# WinForms

This document describes code for dynamically adding and removing rows from a DataGridView control. When the form loads, it adds 3 columns and 1 row to the DataGridView. It makes the first column read-only and populates it with row numbers. When a cell finishes editing, if it is not the first column, the row number is updated. When rows are removed, it renumbers the remaining rows.

Uploaded by

bikash87
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views1 page

Dynamic GridView in C# WinForms

This document describes code for dynamically adding and removing rows from a DataGridView control. When the form loads, it adds 3 columns and 1 row to the DataGridView. It makes the first column read-only and populates it with row numbers. When a cell finishes editing, if it is not the first column, the row number is updated. When rows are removed, it renumbers the remaining rows.

Uploaded by

bikash87
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms;

namespace DynamicGridView { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataGridViewTextBoxColumn c1 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn c2 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn c3 = new DataGridViewTextBoxColumn(); dataGridView1.Columns.Add(c1); dataGridView1.Columns.Add(c2); dataGridView1.Columns.Add(c3); dataGridView1.Rows.Add(); dataGridView1.Columns[0].ReadOnly = true; } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex > 0) { int i = e.RowIndex; dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString(); } } { private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) for(int i=0;i<dataGridView1.Rows.Count-1;i++) { dataGridView1.Rows[i].Cells[0].Value=i+1; } }} }

You might also like