DataGridView with first empty row

feurich

Centurion
Joined
Oct 21, 2003
Messages
170
Location
Holland
Hi There,

I am making a database dialog control that is an add-in to another program. It should give the user the abillity to search from that Database Dialog in to a database. De datagridview is databound to a table via a sql dataset. So the header information is there

Now I would like to keep the first row of the datagridview empty to give the user the possibility to enter in the colomns they want search information and then klik on a button search and the dialog will perform the SQL query with the entered values for the columns.

How can I leave the first row of the datagridview empty?

I have tried almost everything except customizing the datagridview.

If here is a other option please let me know.

All the best.
 
You can insert rows even after binding with [grid].rows.Insert

To bind to a xml file and add an empty row above you can do just this:
Code:
Try
            Dim dset As DataSet = New DataSet()
            dset.ReadXml("sales_by_category.xml")
            DataGridView1.BindData(dset, "category")
            DataGridView1.Rows.Insert(0, 1)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

this snippet inserts a blank row at index 0, the 1 stands for count. Since we want just 1 row, count equals 1.

~DP
 
I tried to do so but I get the message "Rows cannot programmatically be added to the Datagridview's rows collection when the control is data-bound".

The DataGridView control is bound to a DataTable and after that I am trying to add the row.

Is this possible I ask myself and DPrometheus ? :confused:
 
It was not as direct as I thought. The following is the solution for this question:

Code:
DataTable dbTable = new DataTable();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdString,connString);

// Add empty Row to dataTable
DataRow dataRow;
dataRow = dbTable.NewRow();
dbTable.Rows.InsertAt(dataRow, 0);

sqlDataAdapter.Fill(dbTable);

dataGridView1.DataSource = dbTable;
 
Last edited:
Back
Top