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;
Related
I am using DataTable. In which I want to sort on particular columns without creating copy. I have also tried with DataTable.Select() but it required new table for sorted output which will be not worked for me as my source datatable is bound with control.
You can take any example of DataTable
So how can I achieve the task?
Below code will help you to achieve your requirement
IEnumerable<DataRow> data = from dt in dtTemp.AsEnumerable()
orderby dt.Field<Int32>("IDColumn"), dt.Field<String>("NameColumn") ascending
select dt;
Below code as an example:
DataTable dt = new DataTable();
//Populate dt here
dt.DefaultView.Sort = string.Format("{0} {1}", "column name here", "ASC"); //sort ascending
dt = dt.DefaultView.ToTable();
I want to bind the value of groups into DataTable dt1=new DataTable(). After that I want to bind the DataTable datat to DataGrid. But I am unable to do it. When i given the datasource to datagrid as groups directly then I got Exception of "Complex DataBinding accepts as a data source either an IList or an IListSource."
private void BindGrid()
{
var dt = new DataTable();
dt.Columns.Add("Date",typeof(string));
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("City",typeof(string));
dt.Columns.Add("Mobile",typeof(string));
dt.Rows.Add("1/11/2014", "David", "Noida", "Bsnl");
dt.Rows.Add("1/11/2014", "James", "Mumbai", "Airtel");
dt.Rows.Add("30/1/2015", "Ramesh", "Pune", "Vodafone");
dt.Rows.Add("30/1/2015", "Kamal", "Kolkata", "Idea");
dt.Rows.Add("15/5/2015", "Mahesh", "Chennai", "Reliance");
var groups = (
from DataRow row in dt.AsEnumerable()
select new
{
date = row.Field<string>("Date")
}
).Distinct();
DataTable dt1 = new DataTable();
dataGrid1.DataSource = groups;
}
You can bind dt to DataGrid like so:
dataGrid1.DataContext = dt.DefaultView;
In your XAML:
<DataGrid x:Name="dataGrid1" ItemsSource={Binding}"/>
DefaultView is a DataView, which implements IList.
As far as the rest of your code, I've no idea what it is you're doing. For example, what's the purpose of this line:
DataTable dt1 = new DataTable();
You do nothing with dt1.
I have a dataset that i need to add into a dataset. I need to sort datatable before adding it into dataset.I can use DataView to sort the datatable but cannot figure out how to add this dataview into the dataset. Any ideas
DataView myDataView = MyDataTable.DefaultView;
myDataView.Sort = " Key1, Key2"
MyDataSet.Tables.Add(myDataView); // no overload that takes a dataview
You can create a DataTable by calling DataView.ToTable.
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);
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"];