How to Compare Datatables - c#

Background
I have a set of numbers between 1000-9999 split between two data tables both with same the Schema. One data table's schema is then cloned, and then filled with the rows from both of the original tables. This third new data table is then displayed in a datagridview.
Question
How could I then highlight the rows in the datagridview that contain rows from the first data table?
Code
private void combineTables()
{
bothIdsTbl = UsedIdsTbl.Clone();
dataGridView3.DataSource = bothIdsTbl;
for (int i = 0; i <= UnUsedIdsTbl.Rows.Count - 1; ++i)
{
bothIdsTbl.ImportRow(UnUsedIdsTbl.Rows[i]);
}
for (int i = 0; i <= UsedIdsTbl.Rows.Count - 1; ++i)
{
bothIdsTbl.ImportRow(UsedIdsTbl.Rows[i]);
}
dataGridView3.Sort(this.dataGridView3.Columns["stationid"], ListSortDirection.Ascending);
}

The best solution would be the following:
Add a "flag" column to 3rd table before filling it,
Put in that column a value indicating wheteher the row was imported or not from 1st table.
Use the CellFormatting event of the DataGridView to change
cells backcolor according to the flag column.

You can use a command named merge to combine the two datatables.
If this data came from a database you may consider a union query or any other option to combine the data or obtain all the data in one call.

Related

how to show two tables of dataset in gridview and show same row of two tables in single row in c#

here dataset has two tables(table,table1).both have some common fields and common data. while I am binding these two tables into gridview, it shows two rows.
I want to bind in single row,like:https://forums.asp.net/t/1963338.aspx?how+to+merge+two+different+rows+of+datatable+into+single+row+in+same+datatable.
i just created temp(dt4) table. and added required fields from two tables. and finaly binded temp table to grdview.`
if (dt2.Rows.Count > 0)
{
for (int i = 0; i < dt2.Rows.Count; i++)
{
d4.Rows.Add(dt1.Rows[i][0], dt1.Rows[i][1], dt1.Rows[i][2], dt1.Rows[i][3], dt1.Rows[i][4], dt2.Rows[i][3]);
grdVehicleUtilization.DataSource = d4;
grdVehicleUtilization.DataBind();
}
}`

How to add an empty row to a datatable based on value from column

I am working with C# 4.5 on a Winform. I have a DataSet filled with data from my SQL proc that uses a GROUP BY GROUPING SETS that has a row with an aggregate that contains the value "TOTALS" in my second column. I want to insert an empty row after each row that has "TOTALS" in that column. This data will be exported to Excel and I want the empty row between groups for legibility.
Thanks.
for (int i = 0; i < dataTable.Rows.Count; i++)
if (dataTable.Rows[i][0] == "TOTALS")
dataTable.Rows.InsertAt(dataTable.NewRow(), ++i);

datagridview manually filled column value initially not showed

I have an application with multiple datagridviews in a tabcontrol.
One of the tabcontrols has 4 columns that are filled by the datasource.
I don't use autogenerate columns, but use the DataPropertyName to bind values to the columns.
I also have 2 columns i create, wich are filled manually, after assigning the DataSource.
Values in this datagridview are filtered values from a datagridview somewhere else in the application.
When i filter, then go to the tab containing the datagridview, the manual columns are empty.
Changing the filter (but not the results) fills those colums.
How can i get the columns that are manually filled to always show their values?
Calling Refresh() or Update() on the datagridview doesn't solve my problem
Short version of my code:
I actually use a class that inherits from DataGridView
//fill datagridview
PcbLink[] links = Service.Instance.Client.queryPcbLinks();
List<Pcb> pcbs = new List<Pcb>();
foreach (PcbLink link in links)
{
PcbFilter pcbfilter = new PcbFilter();
pcbfilter.pcb_id = link.pcb_id;
Pcb[] res = Service.Instance.Client.queryPcbs(pcbfilter);
pcbs.Add(res[0]);//only first element because pcb_id should always be unique -> only one row
}
cdgvUsage.SetData(pcbs);//setData is used as cdgvUsage.DataSource = pcbs, but does some stuff internally
for (int i = 0; i < links.Length; i++)
{
SortableBindingList<Pcb> dataSource = cdgvUsage.GetData<Pcb>();
cdgvUsage["count", i].Value = links[i].count;
char variantchar = (char)('a' + (char)(dataSource[0].variant));
cdgvUsage.Columns["variant"].ValueType = typeof(string);
cdgvUsage["variant", i].Value = variantchar.ToString();
}
I believe after asigning DataSource to the dataGridView changes must be saved in datasource also otherwise they will not be reflected in the dataGridView (as it takes data from datasource everytime you do Refresh() or Update()).
Make sure manually filled values are stored in dataSource.

dataGridView population from dataSet

I cannot get my dataGridView to update with all the tables in the dataSet. I just get one row in the dataGridView after I try to populate it.
I call this to fill my dataSet:
DataTable table = new DataTable(DateTime.Now.ToString());
table.Columns.Add("c1");
table.Columns.Add("c2");
table.Columns.Add("c3");
table.Columns.Add("c4");
table.Columns.Add("c5");
table.Columns.Add("c6");
table.Columns.Add("c7");
table.Rows.Add("s1", "s2", "s3", "s4", "s5", "s6", "s7");
dataSet1.Tables.Add(table);
dataSet1.WriteXml("MyData.xml");
The data is written fine, but if I try to read it with this I only get one row populated, even if there's multiple entries in the dataSet.
dataSet1.ReadXml("MyData.xml");
MessageBox.Show((dataSet1.GetXml()));
I get the message with the data in the correct format, with the correct number of entries.
dataGridView2.AutoGenerateColumns = true;
int i2 = 0;
while (i2 < dataSet1.Tables.Count)
{
dataGridView2.DataSource = dataSet1.Tables[i2];
i2++;
}
After this it will only show one row and not the amount of tables. The dataSet1.Tables.Count >= 2, but still only get 1 row.
Edit: It seems that each time I call this line:
dataGridView2.DataSource = dataSet1.Tables[i2];
It is deleting the previous row. Is there any way to append the row instead?
You only created one table in the data set, so your while statement only executes once:
while (i2 < dataSet1.Tables.Count)
{
...
}
Count = 1 (number of tables) because you only called dataSet1.Tables.Add(table); one time.
dataSet1.WriteXml("MyData.xml") is only writing out the schema for that single table. So reading it back in will only read back in a single table's worth of schema.
The following code appears to count the number of tables in the dataset:
while (i2 < dataSet1.Tables.Count)
Try pointing this to the table:
dataSet1.Tables["Yourtable"].Count

Spit DataTable into two by Rows

I have one asp.net datatable and I want to databind into two asp.net datalists, so I though to slice the datatable rows in two datatables both the same size if even .
Use the Take LINQ extension method to specify how many items to use.
And the Skip to jump over if needed.
var half = myList.Take(myList.Count / 2);
If you are slicing by rows, you can simply create a copy of your original data table, find a suitable half way point, and just import the rows into the copy, while deleting them from the original.
Something like the following should work:
DataTable originalTable = new DataTable();
//Load the data into your original table or wherever you get your original table from
DataTable otherTable = originalTable.Copy(); //Copys the table structure only - no data
int rowCount = originalTable.Rows.Count;
int wayPoint = rowCount / 2; //NB integer division rounds down towards 0
for(int i = 0; i <= wayPoint; i++)
{
otherTable.ImportRow(originalTable.Rows[i]); //Imports (copies) the row from the original table to the new one
originalTable.Rows[i].Delete(); //Marks row for deletion
}
originalTable.AcceptChanges(); //Removes the rows we marked for deletion

Categories