i have two tables: DataTable dt_o; and DataTable dt_m;
Contents of dt_m:
ID | Name | Age
--------------------------------------
08 | Farel | 18
07 | Ema | 18
10 | Sophie | 19
11 | Cyril | 12
Contents of dt_o:
ID | Name | Age
--------------------------------------
08 | Farel | 19
07 | Ema | 18
10 | Sophie | 19
the row from dt_m with the ID of 08 should be set to row.SetModified() since once of it's columns value where different in the other Table[dt_o]
and the row from dt_m with the ID of 11 should be set to row.SetAdded() since it doesn't exist in the other table...
and the rest of the row, there row states would be set to unchange state..
any code suggestion?
I would search the tables for the specified requirements you're seeking using the answer of this post: how to search the dataset for specific data
Then just set the state of the rows returned based on what you wanna do. For example:
dataSet.AcceptChanges(); //If you'd like to start with everything as "unchanged"
foreach(DataRow row in rowsReturnedFromQuery1)
row.SetAdded();
foreach(DataRow row2 in rowsReturnedFromQuery2)
row2.SetModified();
Hope this helps. I'm sure you can do this using Linq as well.
Related
I am having trouble filtering a DataTable, say DtFromExcel. The DataTable does NOT have a header row, and it starts with an actual data row, and it looks something like the following.
1 | 05/01/2020 Fri | ABC | XYZ | ...
2 | 05/01/2020 Fri | AAA | WKV | ...
3 | 05/02/2020 Sat | BCD | OPQ | ...
4 | 05/03/2020 Sun | CDE | RST | ...
5 | 05/03/2020 Sun | EFA | FAY | ...
6 | 05/03/2020 Sun | AXG | EAS | ...
7 | 05/04/2020 Mon | DEF | LMN | ...
8 | 05/04/2020 Mon | SXA | YTR | ...
9 | 05/05/2020 Tue | DAF | AAG | ...
The second column contains a certain date with some extra string (day of the week), and these rows are ordered by this date column. There can be multiple rows with the same date.
Now, I want to delete rows where the date column contains a certain date AND any rows prior to that. For example, if the certain date is 05/04/2020, then I need to delete all rows up to the 8th row, so that the remaining DataTable would have to look like
9 | 05/05/2020 Tue | DAF | AAG | ...
My problem is, first I don't know how to filter the DataTable without the column name. I thought about assigning a header row without overwriting the first actual data row, but it seems like this is a lot of work, only to filter. Second, I am not sure how to use these conditions ((a)the second column contains a certain date, AND (b)any row with the dates prior to that certain date).
private void DeleteRows(DateTime certainDate){
DataRow[] targetRowsToDelete = dtFromExcel.Select(/* Not sure what to put in here */);
foreach (DataRow row in targetRowsToDelete)
{
if (Convert.ToDateTime(row[1].ToString().Split(c" ")[0]) <= certainDate)
DtFromExcel.Rows.Remove(row);
}
}
I did not want to loop through the whole DataTable because this process occurs often in my program.
If you use the empty constructor to create a DataColumn with no name, the documentation states...
When created, a DataColumn object has no default ColumnName or Caption. When you add it to a DataColumnCollection, a default name ("Column1", "Column2", and so on) will be generated if a name has not been assigned to the ColumnName.
...so creating and loading a DataTable like this...
const string Input = #"1 | 05/01/2020 Fri | ABC | XYZ | ...
2 | 05/01/2020 Fri | AAA | WKV | ...
3 | 05/02/2020 Sat | BCD | OPQ | ...
4 | 05/03/2020 Sun | CDE | RST | ...
5 | 05/03/2020 Sun | EFA | FAY | ...
6 | 05/03/2020 Sun | AXG | EAS | ...
7 | 05/04/2020 Mon | DEF | LMN | ...
8 | 05/04/2020 Mon | SXA | YTR | ...
9 | 05/05/2020 Tue | DAF | AAG | ...";
DtFromExcel = new DataTable();
for (int i = 0; i < 5; i++)
{
DataColumn column = new DataColumn();
Console.WriteLine($"Column {i} has ColumnName \"{column.ColumnName}\"");
DtFromExcel.Columns.Add(column);
Console.WriteLine($"Column {i} has ColumnName \"{column.ColumnName}\"");
}
foreach (string line in Input.Split("\r\n"))
{
string[] fields = line.Split(" | ");
DtFromExcel.Rows.Add(fields);
}
...produces this output...
Column 0 has ColumnName ""
Column 0 has ColumnName "Column1"
Column 1 has ColumnName ""
Column 1 has ColumnName "Column2"
Column 2 has ColumnName ""
Column 2 has ColumnName "Column3"
Column 3 has ColumnName ""
Column 3 has ColumnName "Column4"
Column 4 has ColumnName ""
Column 4 has ColumnName "Column5"
...so you could always use those default names. Further, just because your input data doesn't specify column/field names doesn't mean you can't do so after it's been loaded into the DataTable...
DtFromExcel.Columns[1].ColumnName = "MyDateColumn";
Either way, you'll have a known name by which you can refer to that column.
As to your comment about not wanting to "loop through the whole DataTable", it's not clear if you mean because of the additional code or perhaps performance implications, but to the latter point even if you don't explicitly loop through and test every DataRow, Select() will. On that note, since you say the rows are ordered by date, you can exploit that using LINQ to stop scanning rows as soon as a date outside the search range is found...
private static DateTime GetRowDate(DataRow row) => DateTime.ParseExact(
(string) row["MyDateColumn"], "MM/dd/yyyy ddd", null
);
private void DeleteRows(DateTime maxDate)
{
DataRow[] rowsToRemove = DtFromExcel.AsEnumerable()
.TakeWhile(row => GetRowDate(row) <= maxDate)
.ToArray();// Required to prevent "Collection was modified" exception in foreach below
foreach (DataRow row in rowsToRemove)
DtFromExcel.Rows.Remove(row);
}
If your rows aren't guaranteed to be sorted by date, then you can substitute Where() for TakeWhile() and it will work just the same.
As for your original request to use DateTable.Select(), I'm not sure if that's even feasible here since your dates appear to be stored as string, not DateTime, in your DataColumn. I see that the expression syntax supports a CONVERT() function that can convert between String and DateTime, but I can't imagine that would be any more performant or readable than LINQ so I wouldn't pursue that unless you absolutely have to.
I have a gridview which has 2 header rows as below
| Name | Subject1 | Subject2 |
| | T | V | T | V |
|-------|-----|-------|-----|-------|
| John | 80 | 20 | 78 | 18 |
| Ann | 75 | 18 | 68 | 15 |
The grid view data source is a data table which i have created dynamically. Header rows are also created dynamically.
Now I want to set tooltip for the second header row as 'Theory' for 'T' and 'Viva' for 'V'.
By using gvMarks.HeaderRow.Cells[1].Tooltip, I am able to set tooltip only for the first header row.
If I use gvMarks.Rows[1].Cells[1].Tooltip, I am able to access only the grid data rows.
How can I access the second header row so that I can set the tooltip programatically.
Please refer to the simple table below.
I don't know how to save all data.
GRIDVIEW:
COLUMN1| COLUMN2 | COLUMN3 | COLUMN4 | COLUMN |....
ROW1 | 100 | 105 | 109 | 112 |
ROW2 | 55 | 58 | 62 | 67 |
ROW3 | 71 | 75 | 79 | 83 |
ROW4 | 204 | 209 | 216 | 221 |
ROW..
gridviewRowsColumns
Notes:
First column = stock code, (in tag property, contain ID)
Columns COLUMN1, COLUMN2, COLUMN3.... sales prices for stock.
I have a Visual Studio Program where the user enters specific data of production for that day. Example:
Date | Part Number | Mold Num | Machine Num | Total Parts |Cycle
2/12/2016 | 1185-5B8 | 6580 | 12 | 220 | 56
2/12/2016 | 2249300 | 7797 | 36 | 600 | 13
2/12/2016 | 146865 | 5096789 | 56 | 500 | 15
2/16/2016 | 123456 | 7787 | 54 | 300 | 34
2/16/2016 | 123456 | 787 | 54 | 360 | 36
2/16/2016 | 123456 | 777 | 54 | 500 | 46
2/16/2016 | 123456 | 87 | 54 | 400 | 44
I'm trying to have people enter production data and then manipulate it in order to get the machine usage (MU) (Equation: Total Parts/(3600/Cycle). I want to get these things for each day and for a specific month when I call it. I looked at what others have done but they are just counting how many are in a specific date but I need to multiply, and divide other columns together in two different tables with the same dates in order to get what I need.
Example output:
2/12/2016: MU = 41.12%
2/16/2016: MU = 24.79%
February: MU = 32.96%
EDIT:
I would like to do something in the lines of this example but I do not know how to implement it to an already existing DataSet.
Grouping and Sum Datatable by two fields with different Where conditions
or this example:
Calculating percentage of a groups from datatable
I'm using MySQL.
This is table name item_supplier
supplier_ID Item_ID Date Price QTY
1 1 2012-01-01 00:00:00 500.00 2
1 1 2012-01-03 00:00:00 450.00 10
2 1 2012-01-01 00:00:00 400.00 5
3 1 2012-05-01 00:00:00 500.00 1
I need a select query showing a table something like this.
supplier_ID 2012-01-01 2012-01-03 2012-05-01
1 500.00(2) 450.00(10) null
2 400.00(5) null null
3 null null 500.00(1)
or, at least,
supplier_ID 2012-01-01 2012-01-03 2012-05-01
1 500.00 450.00 null
2 400.00 null null
3 null null 500.00
I hope someone can help me on this or give me a hint.
If there aren't a finite number of dates that are known beforehand, then you can't do what you want in MySQL alone.
Your best bet is to get a table like:
+---------+------------+-------------+-------------+
| Item_ID | Date | supplier_ID | price |
+---------+------------+-------------+-------------+
| 1 | 2012-01-01 | 1 | 500.00 (2) |
| 1 | 2012-01-01 | 2 | 400.00 (5) |
| 1 | 2012-01-03 | 1 | 450.00 (10) |
| 1 | 2012-05-01 | 3 | 500.00 (1) |
| ... | ... | ... | ..... |
Which can be done with:
SELECT Item_ID,Date,supplier_ID,CONCAT(FORMAT(Price,2),' (',QTY,')') AS price
FROM item_supplier
ORDER BY Item_ID,Date,supplier_ID;
Then on the C# side, loop through the results and print your desired output.
Since the output is now sorted by Item_ID, Date, and then supplier_ID, it's simple to loop through the results and then output in the format you want.
First of My SQL does not support to Crosstab/Pivot Query. So you need to create Dynamic temp Table for Columns and then inset record into it.
Like, First you have to fetch all date in one cursor and then create temp table and insert columns based on date's Cursor . After Creating table create another cursor for inserting rows. and fetch every row and update temp table.I have also done this using this way....
If you have any query please contact.