How do you automatically resize columns in a DataGridView control AND allow the user to resize the columns on that same grid?

This trick works for me: grd.DataSource = DT; // Set your desired AutoSize Mode: grd.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; grd.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; grd.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; // Now that DataGridView has calculated it’s Widths; we can now store each column Width values. for (int i = 0; i <= grd.Columns.Count – 1; i++) { // Store Auto Sized … Read more

right click context menu for datagridview

You can use the CellMouseEnter and CellMouseLeave to track the row number that the mouse is currently hovering over. Then use a ContextMenu object to display you popup menu, customised for the current row. Here’s a quick and dirty example of what I mean… private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) … Read more

How to add a new row to datagridview programmatically

You can do: DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[0].Value = “XYZ”; row.Cells[1].Value = 50.2; yourDataGridView.Rows.Add(row); or: DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[“Column2”].Value = “XYZ”; row.Cells[“Column6”].Value = 50.2; yourDataGridView.Rows.Add(row); Another way: this.dataGridView1.Rows.Add(“five”, “six”, “seven”,”eight”); this.dataGridView1.Rows.Insert(0, “one”, “two”, “three”, “four”); From: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx