I want a user to have different "views" of a TableAdapter. These do not need to be committed back to the database.
I have tried something like the following
mytba.DataView = someOtherDataView;
with no success.
Is it possible to do this?
You need to create a new DataView from the underlying DataTable.
It's been a while since I used TableAdapters, but you can fill a data table with it
DataTable dt = new DataTable();
MyTableAdapter.Fill(dt);
DataView dv1 = new DataView(dt);
DataView dv2 = new DataView(dt);
Related
I want to sort my datagrid view column which is non data bound. so please help me . following is the code
private void dgvDailyEntry_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
this.dgvDailyEntry.Rows[e.RowIndex].Cells["sno"].Value = (e.RowIndex + 1).ToString();
}
sort
Thanks in Advance
If it's not bound, I'd assume you have a datatable assigned as datasource. If you can sort the data while obtaining the datatable, the best is to sort it right there. If you can't (i.e. data are generated in random order), sort the datatable then.
C#
Datatable dt; // that is the source datatable
DataView SortedDataView = new DataView();
SortedDataView = dt.DefaultView;
SortedDataView.Sort = "COlumnNameToSortBy DESC";
dt = SortedDataView.ToTable();
VB.NET:
Dim dt as Datatable ' that is the source datatable
Dim SortedDataView As New DataView
SortedDataView = dt.DefaultView
SortedDataView.Sort = "COlumnNameToSortBy DESC"
dt = SortedDataView.ToTable()
If you somehow don't have datatable, I'd certainly recommend to implement it. It's a good practice even if you don't need sorting and such.
private void btnSearchDB_Click(object sender, EventArgs e)
{
OleDbConnection accessConnect = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\ECM\ECM\ECM\ECM.mdb");
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ECMeasurements WHERE [Job Number] LIKE " + txtJobNumber.Text, accessConnect);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
There are also choices that can be searched by Date.....(txtDate.Text) and a comboBox (cbAlloyyTemper.Text). Do I write three different queries or can all the search criteria be together?
First: never use strings in the queries, but use Parameters.
Second: I would create 3 different queries, for easy debugging after. But all depends how much queries you can have, and your own comfort feeling.
This is not the way I would do this at all.
If you are looking at the same table each time you generate each query then you don't want to be querying the database each time. Instead you should load the data into a Data Table, and then query the Data Table in memory rather than the Database each time.
You need to create a Binding Source, and then set the data source for this object to your Data Table object.
For you gridView, you set the datasource for this object to your Binding Source.
Like so :-
OleDbConnection accessConnect = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\ECM\ECM\ECM\ECM.mdb");
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ECMeasurements", accessConnect);
da.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
Now, any change in the state of the datatable reflects through to your gridview automatically.
To Query the DataTable you can use a DataView. DataView's allow you to search the Datatable with parameters like an SQL Statment. The syntax is off the top of my head, but its the method that you need to look at.
Look at Querying DataTables, using DataViews. There are many examples on the Internet.
In your scenario, its best suited.
Good Luck.
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;
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"];
I'm using the following code to populate a DataGridView (sqliteAdapter derives from DbDataAdapter):
sqliteAdapter.SelectCommand.CommandText = SQLCommand;
sqliteConn.Open();
using (DataTable dt = new DataTable())
{
sqLiteAdapter.Fill(dt);
dataGridRes.DataSource = dt;
}
The Actual SQLCommand is:
SELECT Email NOTNULL AS Sel, Regiao, Distrito, Grupo, MG, ID, Nome, Morada, Email
FROM assessores
Now, I would like to reuse this DataTable to populate other controls in the form. Namely 4 CheckedListBox controls. I was hoping to avoid any further connections to the database by "filtering" the current DataTable to something like (Invalid code. Illustrative only)
SELECT Distinct Regiao FROM DataTable
SELECT Distinct Distrito FROM DataTable
SELECT Distinct Grupo FROM DataTable
SELECT Distinct MG FROM DataTable
My forays into adapter and DataTable properties and methods have been fruitless.
Controls can be bound to any collection, not just a DataTable.
You can therefore use LINQ, like this:
myControl.DataSource = dt.AsEnumerable().Select(dr => dr.Field<string>("Regiao")).Distinct().ToArray();
You can filter a DataTable but it returns an array of DataRow objects rather than a filtered DataTable, which can't be directly bound to a DataGrid or other data bound control. Instead use a DataView:
DataView dv = new DataView(dt);
dv.RowFilter = "Filter Expresion";
dg.DataSource = dv;
See Sorting and Filtering Data Using a DataView