How can I divide a dataset equally into two parts? - c#

I'm programming on Asp.net and C#.
I have a Dataset which is populated by records from database.
On my design page, I have two Datagrids.
How can I equally divide the records inside the Dataset so they can be bound to two separate datagrids?

you can use DataView on your dataset and then bind your 2 Datagrids on dataview
here a sample
http://www.dotnetperls.com/dataview
work for instance to set one dataview on rows.count/2 and the other too

This works for me,
var d=ds.Tables[0];// here ds is your dataset.
int count=d.Rows.Count;
var x=new DataTable();
for(int i=0;i<=count;i++)
{
var dr=d.Rows[i];
x.Rows.Add(dr.ItemArray);
d.Rows.RemoveAt(i);
}
var ret=new DataSet();
ret.Tables.Add(x);
ret.Tables.Add(d);
so now you have dataset that contain two equal datatable.

Related

DataGridView c#, making a new a custom "fill" method

I have a table on the db, which is shown on 2 different datagridviews, but, one is filled by default, showing all the data on the table, and the second one I want to be filled with a condition.
So I'd like to fill that table initialy with a custom select query. How can I achieve that?
pd: I use datasets, in which I created the custom query, so the question should be how do I fill by default a table with certain tableadapter I made.
if you can share your codes, you will find a most right answer quickly.
var query = db.table.Where(x=>x.column1==condition1).ToList();
dataGridView1.DataSource = query (); dataGridView1.DataBind();
Or, if you have filled datatable/dataset you can write
dataGridView1.DataSource = yourDataTable; dataGridView1.DataBind();
You want to filter the data in your DataSet then set the DataSource of your DataGridView to a DataTable created by that filter. The code would look something like:
// Need to create a DataView from your original DataSet
// and set the view to the default view from your DataSet.
DataView view = new DataView();
view = myDataSet.Tables[0].DefaultView;
// Next, create a filter on that view
view.RowFilter = "State = 'CA'";
// Now create a DataTable from the view
// and set the DataSource of the DataGridView to that DataTable.
DataTable dt = view.ToTable("tablename");
dgrdToDisplay.DataSource = dt;

Filtering a DataTable and inserting the result into another datatable

I am working on a dataset with has several records in it and I have a method which accepts a datatable as an input parameter.
For example, I have a dataset named dsDetails and one of the table in it is Charges with the following data
Type Rate Name
B 14 bbb
A 10 ABC
C 12 ccc
I am passing the above datatable to my c# method as follows
Populate(dsDetails.Tables["Charges"]);
Everything looks fine, but now that I want to filter the above datatable by type and want to pass the datatable with records of Type=A
May I know good way to do that to pass a filtered datatable?
You probably want to use a DataView object for that:
DataView dv = new DataView(dsDetails.Tables["Charges"]);
dv.RowFilter = "Type = 'A'";
Populate(dv.ToTable());

how to add new datatable to dataset at first position

i have one dataset with 3 data tables again i have to add one more data table in same dataset at first position(Ex:mydataset.tables[0]th position) .can any one help me regarding this.
You will probably need to pull all the datatables out of the dataset into a list, get them in the right order, and then re-add them all to the dataset since you cannot insert to or modify the existing order:
var tables = new DataTable[4];
tables[0] = mynewtable;
tables[1] = mydataset.Tables[0];
tables[2] = mydataset.Tables[1];
tables[3] = mydataset.Tables[2];
mydataset.Tables.Clear();
mydataset.Tables.Add(Tables[0]);
mydataset.Tables.Add(Tables[1]);
mydataset.Tables.Add(Tables[2]);
mydataset.Tables.Add(Tables[3]);

Combine 2 tables in same datset into 1 table

Ok, I have searched and searched debugged guessed and tried hundreds of different ways.
I have an xml Style webresponse read into a dataset and am trying to display 3 of the 10 created tables of the dataset in a datagridview.
How does this get done?
I can display any 1 of the tables but how can i display more then one without any keys? Just matching the rows of each table to each other in numerical order?
StreamReader sreader = new StreamReader(rsp.GetResponseStream());
string rspXml = reader.ReadToEnd();
StringReader srxml = new StringReader(rspXml);
DataSet ds = new DataSet("Shipment");
ds.ReadXml(srxml);
dataGridView1.DataSource = ds;
dataGridView1.DataMember ="TotalCharges";
the three tables I need are "TotalCharges" "Shipment" and "Payments"
I think you should use DataSet.Merge() to create a new DataSet containing the data you want to display, then bind your dataGridView1 to that.

How to get an underlying DataTable from a control (i.e. under DataGridView)?

dataGridView1.DataSource = myDataSet1;
dataGridView1.DataMember = "SomeTable";
And now I want to get the refference to DataTable back from my dataGridView1.
Something like this:
DataTable dt = (DataTable)dataGridView1.DataSource... ;
I'm aware of BindingContext, but couldn't find the way to get DataTable refference back.
Got it.
DataSet dataSet = (DataSet)dataGridView1.DataSource;
string tableName = dataGridView1.DataMember;
DataTable dt =dataSet.Tables[tableName];
you are assigning Dataset as datasource to your gridview. So, the line below would help u.
DataTable dt = ((DataSet)dataGridView1.DataSource).Tables[index];
Assuming that you have only one datatable in your dataset. you can also use your table name instead of index.
You can convert it in this way
BindingSource bs = (BindingSource )dgrid.DataSource;
DataTable tCxC = (DataTable ) bs.DataSource
Look at this question How can I export a GridView.DataSource to a datatable or dataset?
You can either be resilient (to avoid null error) or take a chance.
The short version is:
DataTable dt = ((DataSet) dataGridView1.DataSource).Tables[0];
A more resilient approach (not assuming the view is bound to a DataSet):
DataSet ds = dataGridView1 as DataSet;
if (ds != null) DataTable dt = ds.Tables[0];
Obviously you can inspect/check the number of tables in the DataSet.

Categories

Resources