select top N rows from Datatable C# Windows form application - c#

I am using following code to select top 1000 rows from Datatable dt_Customers and update it with this selection. Every thing is working fine.
DataTable dt = new DataTable();
dt = dt_Customers.Rows.Cast<System.Data.DataRow>().Take(1000).CopyToDataTable();
I am not sure this is the right way or not? Is there any other way to achieve this or I am going fine ?
Thanks.

You don't need to instantiate new DataTable and assign it to dt variable. Also you can use AsEnumerable() extension:
DataTable dt = dt_Customers.AsEnumerable().Take(1000).CopyToDataTable();

Related

ADO.NET DataTable sort does not reflect in containing DataSet

I want to sort a DataTable within a DataSet. I have the following code:
DataTable dt = ds.Tables[0];
dt.TableName = "NEWNAME";
dt.DefaultView.ApplyDefaultSort = false;
dt.DefaultView.Sort = "COL1 desc";
dt = dt.DefaultView.ToTable();
dt.AcceptChanges(); // <-- Break Point Here
ds.AcceptChanges();
As I step through the code beyond the break point in Visual Studio, checking on dt in VS visualizer shows the sorted table, but then checking on ds in VS visualiser does not show the table data in sorted order, even though the name change is reflected. I have tried multiple ways of sorting the datatable available in a google search but the outcome remains the same.
What am I doing wrong?
DefaultView.Sort doesn't sort anything. It is just a string that will be used when you require the construction of a new table like you do in the line
dt = dt.DefaultView.ToTable();
after this point the dt reference (correctly sorted with the info taken from DefaultView.Sort) is no more pointing to the ds.Tables[0]. It is an entirely new DataTable.
The other way in which the DefaultView.Sort applies is when you loop through the DefaultView like in
foreach(DataViewRow dvr in ds.Tables[0].DefaultView)
{
// Here you get a row from the table sorted according to the property.
DataRow row = dvr.Row;
.....
}

How to get particular DataTable using TableName?

One DataSet usually has lots of DataTable, but I'm only to target a particular DataTable which I thought it's pretty normal but apparently failed to do it?
Below were the approaches I've tried:
//Could not find an implementation of the query pattern for source type.......
DataTable dt = from table in changesDataSet.Tables
where table.TableName = "ABC"
select table;
//Surprisingly there was no method "Where" in changesDataSet.Tables
DataTable dt = changesDataSet.Tables.Where(x=>x.TableName="ABC").First();
Below was the code that able to print each and every table. I know I can do it via a loop but please tell me loop isn't the only options
foreach(DataTable table in changesDataSet.Tables)
{
Console.WriteLine(table.TableName);
}
You can access the table using an indexer on the collection of tables (DataTableCollection):
DataTable dt = changesDataSet.Tables["ABC"];

How to transfer a DataTable between pages

I have a DataTable at page1.aspx and want page2.aspx to read and storage that DataTable so i can freely use this one too.
There is a simple way to do that?
It's only for a college homework so nothig too big or complicated, only a DataTable with simple items.
you can use a Session variable
in page1 code behind
Session["dt1"] = dtFullGrid;
in page2 code behind
GridView1.DataSource = Session["dt1"];
GridView1.DataBind();
or
Datatable dt2 = new Datatable();
dt2 = (DataTable)Session["dt1"];
#NorbertoEscobar offered good solution, but you may use Class to do that. In class you have some function, that will return datatable, or have datatable as it is.
DataTable dt = YourClass.GetMyDataTable();
Or void function:
DataTable dt = new DataTable();
YourClass.FillMyDataTable(dt);

DataTable From Session Memory has No Columns

Im trying to retrieve a datatable that I put into a session variable, however when i do it apears that there are no columns...
I have done this before in VB.NET, I'n now using C#, and it worked perfectly, and as far as i can see there is no change in the code other than the obvious syntax changes.
EDIT:
The dt (in part 2) variable has the right data when i look in the data visulization window, but i have noticed that the other properties associated with a datatable are abscent. When i hover over a normal looking datatable with my mouse it looks like the following: " dt ----- {System.Data.DataTable}" but in the class im working on it just looks like "dt ----- {}". Also, after I return the session variable into the dt I clone it (dtclone = dt.clone(); ) and the clone is empty in the data visulizer.... what on earth!
EDIT 2:
I have now also tried converting the first datatable to a dataset, putting this in the session variable, and recoverting it back to a datatable in the class.
Am starting to wonder if it is a probelm with:
dt.Load(sqlReader);
The data does appear after this step though in the dataset visualiser, but not after being cloned.
Code below.
All help very welcome!
Thanks in advance
Chris
1) SQL command in a webhandler, the results of which populate the datatable to be put into the session variable.
DataTable dt = new DataTable();
SqlDataReader sqlReader= default(SqlDataReader);
SqlDataAdapter sqlAdapter = new SqlDataAdapter();
sqlReader = storedProc.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(sqlReader);
System.Web.HttpContext.Current.Session["ResultsTable"] = dt;
2) Part of the code in a class which performs calculations on the table:
DataTable dt = (DataTable)HttpContext.Current.Session["ResultsTable"];
did you call DataTable dispose after setting Session Var?
Because if you do this, solution is like this:
System.Web.HttpContext.Current.Session["ResultsTable"] = dt.Copy();

C#.net datagrid

How to add child rows in datagrid c#.net windows forms?
I'm not sure if this is what you're asking, but if you want to append rows you're easiest way is to append them to whatever DataSource you're using before you DataBind()
If this wasn't what you're after, please provide more detail.
Usually you bind the datagrid to a dataset. Could you please clarify what you are looking for so that we can get into more detail?
DataTable myDataTable = new DataTable();
DataGridView myGridView = new DataGridView();
myGridView.DataSource = myDataTable;
DataRow row = myDataTable.Rows.Add(1, 2, 3, 4, 5); //This adds the new row
If you are looking for a nested table, you'll have to go with a third-party control. The DataGridView doesn't support it.
Firstly sure your data GridView name and create DataTable
DataTable DT= new DataTable();
then also create DataRow
DataRow row = new DataRow();
and last call the add() function Like this:
DT.Rows.Add("ID","Name","Addr","number");
and don't miss to set source on DataGrideView for Showing data :
DataGrideView.DataSource = DT;

Categories

Resources