hello I have a code that pulls the data to the gridview using a dataset, what is the best way to check if the Gridview is empty and if it is to not throw an error.. Right now my gridview has the setting to show a message if its empty.. but I just want to null and empty check after attempting to get the data in the dataset
Students students = new Students();
DataSet studentsList = students.GetAllStudents();
GridView1.DataSource = studentsList;
GridView1.DataBind();
If I understand your question correctly, why not just check if the DataSet is empty, before you bind it to the GridView?
If it is, just don't bind it.
DataSet studentsList = students.GetAllStudents();
bool empty = IsEmpty(studentsList); // check DataSet here, see the link above
if(empty)
{
GridView1.Visible = false;
}
else
{
GridView1.DataSource = studentsList;
GridView1.DataBind();
}
You can count the returned row if its has data or not:
DataSet studentsList = students.GetAllStudents();
if(studentList.Tables[0].Rows.Count > 0) //COUNT DATASET RECORDS
{
GridView1.DataSource = studentsList;
GridView1.DataBind();
}
else
{
lblError.Text = "NO RECORDS FOUND!";
}
Regards
Related
I have little problem , I shared images and code below.
I have two datagridview in my code block. First(left side in form) datagridview's name searchDataGridView. This grid shows data from my database by some filters. Second(right side in form) datagridview's name dataGridView1. This grid shows when I click next button if I select some rows from first grid then this rows will add to dataGridView1. This code block working very well but this that my problem when I click next button and selected rows and added to dataGridView1 then searchDataGridVÄ°ew will show new items and selected rows will add to datagridView1, but when I add new items to dataGridView older rows will clear and new selected rows will add but I need that news selected rows must add to under older rows. Thank you for helping.
private void buttonNext_Click(object sender, EventArgs e)
{
Search searchObj = new Search();
DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Company");
dt.Columns.Add("Category");
dt.Columns.Add("Price");
foreach (DataGridViewRow drv in searchDataGridView.Rows)
{
bool chkboxselect = Convert.ToBoolean(drv.Cells["CheckBox"].Value);
if (chkboxselect)
{
dt.Rows.Add(drv.Cells[2].Value, drv.Cells[3].Value, drv.Cells[4].Value, drv.Cells[8].Value);
drv.DefaultCellStyle.BackColor = Color.Gray;
drv.DefaultCellStyle.ForeColor = Color.Aqua;
}
dataGridView1.DataSource = dt;
}
counter +=1;
if (counter == maxIndex)
{
counter =0;
}
try
{
searchCategoryComboBox.SelectedIndex = counter;
}
catch (System.ArgumentOutOfRangeException) // CS0168
{
MessageBox.Show("hata yakalandı");
return;
}
//catch for error outofrange
searchCategoryComboBox.SelectedIndex =counter;
searchObj.Company = searchCompanyComboBox.Text;
searchObj.Category = searchCategoryComboBox.Text;
SearchManager searchMangObj = new SearchManager();
DataTable dt2 = searchMangObj.SearchInfo(searchObj);
searchDataGridView.DataSource = dt2;
textBox1.Text = String.Empty;
}
The problem is the way you get data from the db:
DataTable dt2 = searchMangObj.SearchInfo(searchObj);
searchDataGridView.DataSource = dt2;
Your SearchInfo method returns a new datatable and you bind that so the rows from the previous table are lost. Modify this method so it takes a datatable as a parameter (ie the existing datatable) and add rows to it.
You didn't say how your SearchInfo works to get rows from the db, but I suspect it will use a dataadapter or tableadapter, in which case you should set its ClearBeforeFill property to false to stop it from clearing the search datatable (the table that is databind'd to the searchdatagridview.DataSource)
You would then use it like:
searchMangObj.SearchInfo(searchDataGridView.DataSource as DataTable);
And it would be coded like:
void SearchInfo(DataTable dt){
var dataadapter = blah blah blah;
dataadapater.ClearBeforeFill = false;
dataadapter.Fill(dt);
}
I have a combobox called cmbCaseRemain its data populated from datatable by code
cmbCaseRemain.DataSource = ce.GET_ALL_CASEREMAIN();
cmbCaseRemain.DisplayMember = "caseRemain";
cmbCaseRemain.ValueMember = "idCaseRemain";
and I have a datagridview called dgv_CaseRemain gets its data from another datatable
DataTable dt = new DataTable();
dt = ce.GET_ALL_CASEREMAIN_FILTER(Convert.ToInt32(txtidCase.Text));
dgv_CaseRemain.DataSource = dt;
I'm using the combobox to add items to the datagrid view ... and i want after every adding to filter the items in the combobox so the user can't add the same value twice ... so I created a stored procedure with a parameter
CREATE PROC [dbo].[FILTER_CMB_CASEREMAIN]
#ID int
AS
SELECT
[idCaseRemain], [caseRemain]
FROM
[dbo].[tblCaseRemain]
LEFT OUTER JOIN
tblCase_Link_Remain ON idCaseRemain = idCaseRemain_L
WHERE
[idCaseRemain] <> #ID;
and using a code to pass the parameter which filter the combobox when i click on it
DataTable dt = new DataTable();
dt = ce.FILTER_CMB_CASEREMAIN(Convert.ToInt32(this.dgv_CaseRemain.CurrentRow.Cells[1].Value));
if (dt.Rows.Count > 0)
{
cmbCaseRemain.DisplayMember = "caseRemain";
cmbCaseRemain.ValueMember = "idCaseRemain";
cmbCaseRemain.DataSource = dt;
}
but I got an error
Object reference not set to an instance of an object.
Thank you
( sorry for my bad english :-) )
ok. Got it. As I said, it was error on that line. Use below code. Below I add the validation to check the null value.
int n = 0;
DataTable dt;
if (this.dgv_CaseRemain.CurrentRow.Cells[1].Value != null)
{
if (int.TryParse(this.dgv_CaseRemain.CurrentRow.Cells[1].Value.ToString(), out n))
{
dt = ce.FILTER_CMB_CASEREMAIN(n);
}
}
if (dt!=null && dt.Rows.Count > 0)
{
cmbCaseRemain.DisplayMember = "caseRemain";
cmbCaseRemain.ValueMember = "idCaseRemain";
cmbCaseRemain.DataSource = dt;
}
I don't know what is the syntax for emptying a datagridview. Please help me here is my code.
if (cboProduct.SelectedIndex != -1)
load_variant();
else
//empty the datagridview
cboProduct.SelectedIndex = -1;
set datasource as null
dataGridView1.DataSource = null;
Or
dataGridView1.Rows.Clear()
OR
while (dataGridView1.Rows.Count > 0)
{
dataGridView1.Rows.RemoveAt(0);
}
syntax for emptying a datagridview
Just assign null to its DataSource property.
yourGridView.DataSource = null;
Just set DataGridView.DataSource property to null
Gets or sets the data source that the DataGridView is displaying data
for.
DataGridView1.DataSource = null;
As an alternative (not exactly what .DataSource = null does)
DataTable dt = (DataTable)DataGridView1.DataSource;
if(dt != null)
dt.Clear();
You can set its DataSource to null :
dataGridView.DataSource = null;
Approach 1:
datagridview1.DataSourse=null;
Approach2:
DataView DV = (DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DV.Table.Clear();
Approach 3:
datagridview1.DataSource = ""
Approach 4:
datagridview1.Dispose();//Clears gridview with all its properties
Approach 5:
Using Javascript:
document.getElementById("datagridview1").outerHTML = "";
Hope Its Helpful.
if (dgView.DataSource == null) return;
((DataTable)dgView.DataSource).Rows.Clear();
((DataTable)dgView.DataSource).Columns.Clear();
I used this one to clean datagrid view on my window application.
dgView.DataSource = null is not working for me
Setting the data source to null will also reset all formatting of the DataGridView. You can preserve the formatting by creating an empty DataTable and setting that as the source.
For example:
// Create an empty datatable
DataTable empty = new DataTable();
empty.Columns.Add("Name", typeof(string));
empty.Columns.Add("Color", typeof(myEnum));
empty.Columns.Add("Count", typeof(int));
...
// Clear datagridview
dgv.DataSource = empty;
Try this:
int nn = yourgridview.Rows.Count;
for (int i=nn; i>1; i--)
{
{
yourgridview.Rows.RemoveAt(i-2);
}
}
How can i implement Reordered list by binding data from Database without using datasource
While using database i am getting alert message
"Reorder failed ,see details below .\r\n\r\nCan't Reorder datasource.it is not a datasource and does not implement Ilist"
Try this way,
Create a method to fill the dataset
public DataSet FillDataSet_info(string _param1)
{
try
{
DataSet oDS = new DataSet();
SqlParameter[] oParam = new SqlParameter[1];
oParam[0] = new SqlParameter("#Column_Filed1", _param1;
oDS = SqlHelper.ExecuteDataset(DataConnectionString, CommandType.StoredProcedure, "proc_fill", oParam);
return oDS;
}
catch (Exception e)
{
ErrorMessage = e.Message;
return null;
}
}
Now use above to fill the ListView, GridView, DataList or Repeater
All you have to do is, create a method to bind the data in the GridView or other..
private void GridView_Bind()
{
DataSet oDs_GridView = new DataSet();
string Param1 = "somevalue";
oDs_GridView = oFCC.GetmRoleMaster_infoAll(Param1);
if (oDs_GridView.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = oDs_GridView;
GridView1.DataBind();
}
}
I already know how to specify the datasource but afther doing that it is not populated yet so i was thinking you need some kind of bind() command to populate the comboboxcolumn in the edit form
This below is how i bind the datasource to the comboboxcolumn (and yes i am sure that ds has data rows in it)
(ASPxGridView4.Columns["Naam"] as GridViewDataComboBoxColumn).PropertiesComboBox.DataSource = ds as DataSet;
So could anyone tell me how i can now populate the comboboxcolumn in edit mode?
Edit
protected void ASPxGridView4_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
{
if (dt.Rows.Count < 1)
{
ds = Session["ds"] as DataSet;
}
GridViewDataComboBoxColumn column = (ASPxGridView4.Columns["Naam"] as GridViewDataComboBoxColumn);
column.PropertiesComboBox.DataSource = ds.Tables[0];
column.PropertiesComboBox.ValueField = "Naam";
column.PropertiesComboBox.ValueType = typeof(string);
column.PropertiesComboBox.TextField = "Naam";
}
Here is the code which should work:
DataSet dataSet = ds as DataSet;
GridViewDataComboBoxColumn column = (ASPxGridView4.Columns["Naam"] as GridViewDataComboBoxColumn);
column.PropertiesComboBox.DataSource = dataSet.Tables[0];
column.PropertiesComboBox.ValueField = "SomeValueField";
column.PropertiesComboBox.ValueType = typeof(int); // type of the SomeValueField
column.PropertiesComboBox.TextField = "SomeTextField";
Also, please refer to the GridViewDataComboBoxColumn Class topic.
UPDATE Your code should be implemented in the CellEditorInitialize event as shown below:
protected void ASPxGridView1_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) {
if(e.Editor is ASPxComboBox) {
ASPxComboBox combo = ((ASPxComboBox)e.Editor);
combo.DataSource = dataSet.Tables[0];
combo.TextField = "Naam";
combo.ValueField = "Naam";
combo.DataBindItems();
}
}