clear gridview in vb.net - c#

i am trying to clear the grid view using the following code but it is not working:
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
DataSourceOperation.Delete=GridView1;
connection.Close();`
please tell me what is wrong in this statement.

Try:
GridView1.DataSource = null;
DataSourceOperation is an enumeration so you can't set an Enumeration to be a GridView. How does this even compile?

How about trying this:
dt.Rows.Clear();
GridView1.DataBind();

Related

GridView Column sort in descending

welcome all
I have a simple code that displays the values in a GridView
When sorting, the result is ascending
I want the opposite
enter image description here
I used this code
But I want to reverse the order
GridView1.DataSource = dt;
dt.DefaultView.Sort = "MYPoints";
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
Thanks for help
this should work:
dt.DefaultView.Sort = "MYPoints DESC";

Change a gridview's datasource dynamically

I have two DataTable's which I want to use as DataSoure for a GridView. And when an user clicks on a button1 I need to load the first table and the user clicks button2 the second table should be loaded. My current problem is that the tables don't have the same structure (one has more columns) and when the user clicks button1 first and after clicks button2 the second table is loaded with the right values but it shows the extra columns from table1 as well.
What's the easiest way to fix this?
Instead of manually adding columns set the property DataGridView.AutoGenerateColumns to true. This will cause the grid to create columns according to the data source.
To expand a bit on the Habib answer:
private void LoadTables()
{
// Mock the tables data
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
// Clear data from gridview
GridView1.DataSource = null;
GridView1.DataBind();
// Load first table
GridView1.DataSource = dt1;
GridView1.DataBind();
// Clear data from gridview
GridView1.DataSource = null;
GridView1.DataBind();
// Load second table
GridView1.DataSource = dt2;
GridView1.DataBind();
}

Delete specific row in dynamically generated gridview

I have a small GridView that will hold certain items. This is the code that generates the gridview after a user clicks an item:
if (grdCarritoAddons.Rows.Count == 0)
{
dt.Columns.Add(new DataColumn("Name", typeof(System.String)));
dt.Columns.Add(new DataColumn("Price", typeof(System.String)));
}
else
{
dt = HttpContext.Current.Session["DataTable"] as DataTable;
}
Session["DataTable"] = dt;
DataRow dr1 = dt.NewRow();
dr1[0] = AddonName.ToString();
dr1[1] = Convert.ToDecimal(PriceAddon);
dt.Rows.Add(dr1);
grdCarritoAddons.DataSource = dt;
grdCarritoAddons.DataBind();
}
Now I need a user to be able to select an item and delete a specific one. In the ASPX page I already added the Delete Command and created an empty RowDeleting method in code and on the GridView properties. It isn't working, and I came up with a solution that somewhat works but isn't what I need to do. It basically wipes out the whole DataTable.
//grdCarritoAddons.DataSource = dt;
//grdCarritoAddons.DataBind();
//Session["DataTable"] = dt;
//Session.Remove("Total");
I tried this:
grdCarritoAddons.Rows.RemoveAt(grdCarritoAddons.SelectedRows[0].Index);
But no go. Doesn't work in ASP. Thanks for any help :)
Current Code:
dt = HttpContext.Current.Session["DataTable"] as DataTable;
dt.Rows.RemoveAt(grdCarritoAddons.SelectedRow.RowIndex);
But its throwing an Object reference not set to an instance of an object exception. If I put an IF to check if the row is null it won't execute at all... but the data is obviously not empty.
This is the Error: 'System.Web.UI.WebControls.GridViewRowCollection'
does not contain a definition for 'RemoveAt' and no extension method
'RemoveAt' accepting a first argument of type
'System.Web.UI.WebControls.GridViewRowCollection' could be found
Yes, this method does not exist as you can see here. You should modify the datasource instead and databind the grid afterwards. DataRowCollection has a RemoveAt method.
dt.Rows.RemoveAt(grdCarritoAddons.SelectedRow.RowIndex);
grdCarritoAddons.DataSource = dt;
grdCarritoAddons.DataBind();

Binding Datagridview to dataset

I have a problem binding datagridview to dataset, when I debug code I can see that dataset is filled ok with data from database, but it wont show on datagridview...
Here is my code:
MySqlConnection conn = new MySqlConnection(connectionString);
string sql = #"select artikli.idArtikla, artikli.NazivArtikla
from artikli";
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Artikli");
// Bind the data table to the data grid
dataGridView1.DataSource = ds;
EDIT: Thx for answers, now when I can see my data in grid, what is the easiest way to allow inserting, deleting and editing in my grid and forwarding those cnages to my database table?
DataGridView doesn't know how to bind DataSet, you must bind a DataTable.
dataGridView1.DataSource = ds.Tables["Artikli"];
or
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Artikli";
You need to provide the which DataTable you want to bind to DataGridView.
Try:
dataGridView1.DataSource = ds.Tables[0];
You forgot to bind the dataset to gridview.
At the end of your code add the following line.. after datasource.
dataGridView1.DataBind();

gridView.DataSource as DataTable is setting a null in asp.net

I am setting my gridview.datasource into a datatable variable as below:
DataTable dt = gvPaymentHistory.DataSource as DataTable;
The gvPaymentHistory.DataSource has a record, however, the dt is null after that line has executed. How can I pass the Datasource records to dt?
EDIT
DataSource is List collection of a class object. It's not a DataSet
Simple way is to store the data source of the grid in view source when u r binding the data to the grid and then retrieve it from the view source every time you need it.
Gridview.Datasource = yourdatasource;
ViewState["mydatasource"] = yourdatasource;
While retrieving
DataTable dt = ViewState["mydatasource"] as DataTable;
Hope this solves your problem.
Try This Way: Use BindingSource
BindingSource bs = (BindingSource)gvPaymentHistory.DataSource;
DataTable dt =((YourDataSetType) (bs.DataSource)).Tables[0];
EDIT
if the bidning type is list than try out
List<CodeEntity> data= gvPaymentHistory.DataSource as List<CodeEntity>;
or
List<CodeEntity> codes = (List<CodeEntity>)gvPaymentHistory.DataSource;
check this
if(gvPaymentHistory.DataSource is DataTable)
DataTable dt = gvPaymentHistory.DataSource as DataTable;
if(gvPaymentHistory.DataSource is DataView )
DataView dv = gvPaymentHistory.DataSource as DataView;

Categories