Populate a datagridview with sql query results

Here’s your code fixed up. Next forget bindingsource var select = “SELECT * FROM tblEmployee”; var c = new SqlConnection(yourConnectionString); // Your Connection String here var dataAdapter = new SqlDataAdapter(select, c); var commandBuilder = new SqlCommandBuilder(dataAdapter); var ds = new DataSet(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true; dataGridView1.DataSource = ds.Tables[0];

How could I Drag and Drop DataGridView Rows under each other?

I found this code sample on MSDN Note the following: 1). DataGridView property AllowDrop must be set to true (default is false). 2). The example below works out of the box when the DataGridView is NOT data-bound. Otherwise it will throw an InvalidOperationException. If it is databound, you should manipulate the order of items in … Read more

DataGridView: how to focus the whole row instead of a single cell?

Put this code either into your form’s constructor or set it in datagridview’s Properties using the IDE. dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dgv.MultiSelect = false; dgv.RowPrePaint +=new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint); Then paste the following event into the form code: private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { e.PaintParts &= ~DataGridViewPaintParts.Focus; } And it works! 🙂 “dgv” is the DataGridView in … Read more

Why is my bound DataGridView throwing an “Operation not valid because it results in a reentrant call to the SetCurrentCellAddressCore function” error?

The exception is raised by the DataGridView in order to prevent an infinite loop from occurring. The cause of this is usually one of the following: Changing the active cell while an operation is being performed on the currently-active cell Beginning, ending or cancelling edit mode while a cell edit is already in-progress Any other … Read more

Showing tool tip for every item in datagridview row when mouse is above it

Take a look at the DataGridViewCell.ToolTipText property and use the DataGridView’s CellFormatting event to set this property value. You can use the event’s DataGridViewCellFormattingEventArgs ColumnIndex property to determine if the event is firing for the column you want to set a tool tip for and if so use the event’s RowIndex to specify that tool … Read more

How can I refresh c# dataGridView after update ?

BindingSource is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource are so helpful. If your source is say for example a list of user strings List<string> users = GetUsers(); BindingSource source = new BindingSource(); source.DataSource = … Read more

tech