How can I show my controls even if my table columns are null using gridview? All I know is ShowHeaderWhenEmpty="true"
If datasource of gridview is null, you can create a temporery datatable and assign it as a datasuource of gridview.
if (GridView1.DataSource == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
DataRow dr = dt.NewRow();
dr[0] = "";
dt.Rows.Add(dr);
GridView1.DataSource=dt;
GridView1.DataBind();
}
If your data is NULL then there is no data to show. In order to show the controls, you need to have data.
I'd try to create a dummy row if the db returns null.
if(db.rows.count < 1)
{
//add a row with dummy values
}
Hope this helps.
you can try this:
dataGridView1.Rows.Add(num_rows);
Related
How can I ADD, Delete and Edit multiple rows "containing textboxes" to Gridview
Without inserting to DB
I tried
Gridview1.rows.add(datarow)
And for delete
Gridview1.rows.remove(datarow)
but without detecting selected row
You can use the approach in code below for this. In the addRow method shown, a new grid row is inserted. The logic is that we need to re-bind the grid to a new data source that contains the original rows plus a new empty row. You can use a similar approach for delete ( create a new data source with deleted rows excluded and then rebind the grid).
When deleting use the method deleteRow. I have assumed that you have a check box control with an id of chkDelete in the grid row, which when checked would mean the row needs to be deleted. You can delete multiple rows at the same time using the method of deleteRow.
If you use the below two methods for adding a row and deleting row(s) then automatically your edited text boxes would have their new values retained always i.e. editing would then be automatically taken care of.
Assumptions made : Also, I have assumed that there are 3 text boxes in grid row in addition to a check box. Because there are 3 textboxes, so the DataTable being created in methods below should contain 3 columns for these 3 textboxes and these columns should be of string type.
Add a Row
protected void addRow()
{
DataTable dt = new DataTable();
//add code to create columns for this data table
//only create columns for textbox data
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(string));
DataRow dr = null;
//build a data source of existing rows
foreach (GridViewRow gridRow in grid1.Rows)
{
dr = dt.NewRow();
//set only text box values in new data source
//so checkbox column for row selection will be ignored
TextBox txtColumn1 = gridRow.FindControl("txtColumn1") as TextBox;
TextBox txtColumn2 = gridRow.FindControl("txtColumn2") as TextBox;
TextBox txtColumn3 = gridRow.FindControl("txtColumn3") as TextBox;
dr[0] = txtColumn1.Text;
dr[1] = txtColumn2.Text;
dr[2] = txtColumn3.Text;
dt.Rows.Add(dr);
}
//create the row in data sourec for the new grid row
dr = dt.NewRow();
dt.Rows.Add(dr);
//bind the grid view, which will now show you the new added row in addition to original rows
grd.DataSource = dt;
grd.DataBind();
}
Delete Row(s)
protected void deleteRow()
{
DataTable dt = new DataTable();
//add code to create column for this data table
//only set column for textbox columns
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(string));
//build a data source of existing rows
foreach (GridViewRow gridRow in grid1.Rows)
{
//get whether the checkbox for deleting row is checked or not
CheckBox chkDelete = gridRow.FindControl("chkDelete") as CheckBox;
//do not add original row if it was checked to be deleted
if(!chkDelete.Checked)
{
dr = dt.NewRow();
//set only text box values in new data source
//so checkbox column for row selection will be ignored
TextBox txtColumn1 = gridRow.FindControl("txtColumn1") as TextBox;
TextBox txtColumn2 = gridRow.FindControl("txtColumn2") as TextBox;
TextBox txtColumn3 = gridRow.FindControl("txtColumn3") as TextBox;
dr[0] = txtColumn1.Text;
dr[1] = txtColumn2.Text;
dr[2] = txtColumn3.Text;
dt.Rows.Add(dr);
}
}
//bind the grid view, which will now NOT have the deleted rows
grd.DataSource = dt;
grd.DataBind();
}
I have a grid with two columns Product and Purchase_Date. i get the data from db and show it in grid. if i try to add one more data to this grid on button click using the following code, i get the error "type of value has a mismatch with column ...."
DataTable dt = dataGridView1.DataSource as DataTable;
if (dt != null)
{
DataRow row = dt.NewRow();
dt.Rows.Add(textBox1.Text, dateTimePicker1.value);
dataGridView1.DataSource = dt;
}
How do i rectify it. i tried the following also datetime.parse,convert.todatetime,dateTimePicker1.value.ToString("yyyy-MM-dd hh:mm:ss").No use.
I have used DataGridview, which is supposed to be populated manually (No data sources specified). I want to get its content of first row(index-0). But it gives me null cell values.
Code
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Description");
dt.Columns.Add("Unit Price");
dt.Columns.Add("Quantity");
dt.Columns.Add("Tot");
dr = dt.NewRow();
dr[0] = dataGridViewA_General.Rows[0].Cells[1].Value; //these values are null
dr[1] = dataGridViewA_General.Rows[0].Cells[2].Value; //these values are null
dr[2] = dataGridViewA_General.Rows[0].Cells[3].Value; //these values are null
dr[3] = dataGridViewA_General.Rows[0].Cells[4].Value; //these values are null
dt.Rows.Add(dr);
Thanks in advance
Updated
// dataGridViewA_General
this.dataGridViewA_General.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewA_General.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column_A_item_code,
this.Column_A_description,
this.Column_A_qntity,
this.Column_A_unit_price,
this.Column_A_list_total});
// Column_A_description
this.Column_A_description.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.Column_A_description.HeaderText = "Description";
this.Column_A_description.Name = "Column_A_description";
// Instantiate
private System.Windows.Forms.DataGridView dataGridViewA_General;
private System.Windows.Forms.DataGridViewTextBoxColumn Column_A_description;
(rest of the columns are also defined in the sameway.)
You must add the row to the DataTable.
dt.Rows.Add(dr);
NewRow(); simply creates a blank row, you must add it manually.
Also, as you didn't put anything in the row itself, nothing will be returned.
I had done done a logical mistake and code had not been executed in expected manner after filling data. Actually the above code is correct. Thank you all for your valuable contribution.
i have a DataGridView on the Form. I select some data from database and load them to datatable and after that i make reference this datatable to grid's datasorurce as below.
string sql = "";
sql = "SELECT id,name,surname,code FROM t_persons";
DataTable dt = new DataTable();
...
adapter.Fill(dt);
grid.DataSource = dt;
and after that i want to add new row to this grid with grid.Rows.Add() method. But every time it gives an error Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
So whatis the problem and how can i solve it.
You should add row to the DataTable, not to the DataGridView. That is what the exception is saying. Try:
DataRow newRow = dt.NewRow();
dt.Rows.Add(newRow);
Please you can add row directly to datatable and it's effect on gridview because it's bind to datatable.
I am using a gridview with sqldatasource. How to get back the datasource in the codebehind as a datatable?
Use System.Data.DataTable dt = (System.Data.DataTable)gview.DataSource; if you are binding a DataTable.
You can even extract the DataTable out of DataSet if you are binding DataSet as
System.Data.DataTable dt2 = (System.Data.DataTable)((System.Data.DataSet)gvValidDA.DataSource).Tables[0]; you will have to check the index of your table or table name as you prefer.
Happy coding.
Edited
Use SqlDataSource.Select Method and assign it to a dataview
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
Then extract the datatable using
DataTable dt = (DataTable)dv.ToTable();
The actual data is not stored in the Gridview properties as the above answers pertain.
Using direct casting to the Gridview.DataSource is not enough, as it's actually NULL when the gridview has rendered!
You have to either reload the data directly from the SQL datasource...
DataSourceSelectArguments dss = new DataSourceSelectArguments();
DataView dvS = sdsADDorREMstudentData.Select(dss) as DataView;
DataTable dtS = dvS.ToTable() as DataTable;
if (dtS != null)
{
... etc...
}
Or you can use the ViewState to retain the data before the Gridview has rendered.
DataView dvClasses = (DataView)sdsClasses.Select(DataSourceSelectArguments.Empty);
gvStudents.DataSourceID = "sdsClasses";
gvStudents.DataSource = null; // Null out the source, as we have a SourceID instead
gvStudents.DataBind();
//save the data in a viewstate for later use (to control adding and removing students, without doing a postback! See ADD & REM methods below)
DataView dv = (DataView)dvClasses;
DataTable dt = new DataTable();
if (dv != null)
{
dt = dv.ToTable();
ViewState["gv"] = dt;
}
And use the ViewState to turn it back into a DataTable, when you need to use the data AFTER the Gridview has rendered...
DataTable dt = (DataTable)ViewState["gv"];