I have the following two datatables:
Table[2]
+----+-----------+----------+
| id | FirstName | LastName |
+----+-----------+----------+
| 10 | Fred | Ramirez |
+----+-----------+----------+
| 20 | Willie | James |
+----+-----------+----------+
| 30 | Daniel | Green |
+----+-----------+----------+
| 40 | Matthew | Walker |
+----+-----------+----------+
| 50 | Aaron | Wright |
+----+-----------+----------+
| 60 | Richard | Huey |
+----+-----------+----------+
| 80 | Matthew | Walker |
+----+-----------+----------+
and
Table[3]
+----------+---------------+-----------------+------------------+-------------------+-----------------+-------------------+-------------------+
| Services | Fred ---10--- | Willie ---20--- | Daniel ---30--- | Matthew ---40--- | Aaron ---50--- | Richard ---60--- | Matthew ---80--- |
+----------+---------------+-----------------+------------------+-------------------+-----------------+-------------------+-------------------+
| XXX | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------+---------------+-----------------+------------------+-------------------+-----------------+-------------------+-------------------+
| AAA | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------+---------------+-----------------+------------------+-------------------+-----------------+-------------------+-------------------+
| CCC | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------+---------------+-----------------+------------------+-------------------+-----------------+-------------------+-------------------+
| DDD | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------+---------------+-----------------+------------------+-------------------+-----------------+-------------------+-------------------+
| YYY | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------+---------------+-----------------+------------------+-------------------+-----------------+-------------------+-------------------+
I need to get LastName from the Table[2] and make assign to column name as FirstName + LastName
My code as follows,
int userID = 0;
string[] columnNameParts;
Result.GridViewDataTable = ds.Tables[3];
for (int currCol = 1; currCol < Result.GridViewDataTable.Columns.Count; currCol++)
{
columnNameParts = Result.GridViewDataTable.Columns[currCol].ColumnName.Split(new string[] { "---" }, StringSplitOptions.RemoveEmptyEntries);
userID = int.Parse(columnNameParts[columnNameParts.Length - 1]);
string columnName = ds.Tables[2].Select("Id = " + userID)[0]["FirstName"].ToString() + " " +
ds.Tables[2].Select("Id = " + userID)[0]["LastName"].ToString().Substring(0, 1);
Result.GridViewDataTable.Columns[currCol].ColumnName = columnName;
}
With the above two tables data, I'm getting the following error.
A column named 'Matthew W' already belongs to this DataTable
It happen because Datatable have this (Matthew | Walker) record two times.
Once I setting a column name using this code,
string columnName = ds.Tables[2].Select("Id = " + userID)[0]["FirstName"].ToString() + " " +
ds.Tables[2].Select("Id = " + userID)[0]["LastName"].ToString().Substring(0, 1);
I need to check wether Datatable already consists that column name and if it's already exists I need to set 1 to end of the LastName. If same column name consists multiple time I need to add 1,2,3 likewise end of the LastName. As an example (Matthew W,Matthew W1,Matthew W2,) - how can I do it?
To check the exists column, this should work for you.
Result.GridViewDataTable.Columns.Contains(tmpColumnName)
Then, if you need the number after the columnName, you just put index after it. By the way, this snippet is not the best solution but I hope it can give you an idea of how to solve it. Hope this helps.
int userID = 0;
string[] columnNameParts;
Result.GridViewDataTable = ds.Tables[3];
for (int currCol = 1; currCol < Result.GridViewDataTable.Columns.Count; currCol++)
{
columnNameParts = Result.GridViewDataTable.Columns[currCol].ColumnName.Split(new string[] { "---" }, StringSplitOptions.RemoveEmptyEntries);
userID = int.Parse(columnNameParts[columnNameParts.Length - 1]);
string columnName = ds.Tables[2].Select("Id = " + userID)[0]["FirstName"].ToString()
+ " "
+ ds.Tables[2].Select("Id = " + userID)[0]["LastName"].ToString().Substring(0, 1);
//index
int n = 0;
//Create new temporary columnName which will add to the Columns later
string tmpColumnName = columnName;
//Check if column name is duplicate.
while (Result.GridViewDataTable.Columns.Contains(tmpColumnName))
{
//Add 1 to index
n++;
//Create new name such as Matthew W1,Matthew W2,Daniel G1,Matthew W3,...
tmpColumnName = columnName + n.ToString();
}
//Add new unique column name to Columns
Result.GridViewDataTable.Columns[currCol].ColumnName = columnName;
}
Related
Have two tables:
Catalog
|----------|----------|-----------|
| Id | Name | CreatedBy |
|----------|----------|-----------|
| 1 | Catalog1 | 1 |
| 2 | Catalog2 | 1 |
| 3 | Catalog3 | 1 |
| 4 | Catalog4 | 2 |
|----------|----------|-----------|
TemplateOnCatalog
|------------|-----------|
| TemplateId | CatalogId |
|------------|-----------|
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 4 |
|------------|-----------|
Using c# and Linq, how to get a list of all the catalogs, where CreatedBy = 1, and count the number of templates in the catalog. If no templates, the count value should be 0 (or empty).
Expected result:
|------------|-------------|-------------------|
| CatalogId | CatalogName | NumberOfTemplates |
|------------|-------------|-------------------|
| 1 | Catalog1 | 2 |
| 2 | Catalog2 | 3 |
| 3 | Catalog3 | 0 |
|------------|-------------|-------------------|
I try the following, but it doesn't take catalogs without templates:
var templateCatalogs =
from templateCatalog in db.Catalog.AsNoTracking()
join totc in db.TemplateOnCatalog
on templateCatalog.Id equals totc.CatalogId
where
templateCatalog.CreatedBy == 1
orderby templateCatalog.Name
group new { templateCatalog, totc }
by new { templateCatalog.Name, templateCatalog.Id } into result
select new
{
CatalogId = result.Key.Id,
CatalogName = result.Key.Name,
NumberOfTemplates = result.Count()
};
I need display sum of Columns total (Grand Total) in GridView Footer in ASP.Net using C#.
I have two problems:
I can't apply the css style in GridView Footer;
I have error
Specified cast is not valid.
when add the Sum of Tot2.
How to resolve this?
Thank you advance for any help.
This is SQL query:
sql = " SELECT ZN, ";
sql += " IFNULL(Tot1,0) AS Tot1, ";
sql += " IFNULL(Tot2,0) AS Tot2 ";
sql += " FROM ";
sql += " doTable ";
sql += " ORDER BY ";
sql += " Tot1 DESC; ";
+----+------+------+
| Zn | Tot1 | Tot2 |
+----+------+------+
| ZO | 3 | 0 |
| ZO | 3 | 0 |
| ZO | 2 | 1 |
| ZO | 2 | 0 |
| ZO | 2 | 0 |
| ZO | 2 | 0 |
| ZO | 2 | 0 |
| ZO | 1 | 0 |
| ZO | 1 | 1 |
| ZO | 1 | 0 |
+----+------+------+
10 rows in set
This the code-behind:
OdbcDataAdapter adapter = new OdbcDataAdapter(command);
adapter.Fill(dsProducts);
gvProducts.Columns[1].FooterText = "Total";
gvProducts.Columns[2].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
gvProducts.Columns[2].ItemStyle.CssClass = "ddl_Class_new";
gvProducts.Columns[2].FooterText = dsProducts.Tables[0].AsEnumerable().Select(x => x.Field<Int32>("Tot1")).Sum().ToString();
gvProducts.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
gvProducts.Columns[3].ItemStyle.CssClass = "ddl_Class_new";
gvProducts.Columns[3].FooterText = dsProducts.Tables[0].AsEnumerable().Select(x => x.Field<Int32>("Tot2")).Sum().ToString();
First, you set the css of the footer with FooterStyle.CssClass
gvProducts.Columns[3].FooterStyle.CssClass = "ddl_Class_new";
And specified cast means that there are fields in Tot1 or Tot2 that are not convertible to Int32. Probably because you use IFNULL in your query, change that to ISNULL if you use MS SQL or check if the data in aspnet is what you actually expect it to be by debugging.
how to assign colour to duplicate values in a list
This my table
| user_id | account_no | zip | date |
| 1 | 123 | 55555 | 12-DEC-09 |
| 1 | 123 | 66666 | 12-DEC-09 |
| 1 | 123 | 55555 | 13-DEC-09 |
| 2 | 456 | 77777 | 14-DEC-09 |
| 2 | 456 | 77777 | 14-DEC-09 |
| 2 | 789 | 77777 | 14-DEC-09 |
| 2 | 789 | 77777 | 14-DEC-09 |
You can assign a color in this way:
var userIdGroups = db.TableName.GroupBy(x => x.user_id).AsEnumerable();
var itemsWithColors = userIdGroups
.SelectMany(g => g.Select((x, index) => index == 0
? new { Item = x, Color = Color.Black }
: new { Item = x, Color = Color.Red }));
Now use a foreach loop to process this query and add these items to your UI control.
So I'm attempting to populate a table with seed data in EF5. I have an Enum of all 50 states and DC. I also have a lookup table of RequestTypes with IDs 1-6. It would be something like this:
+----+----------+-------------+------------+
| Id | State | SurveyId | RequestType|
+----+----------+-------------+------------+
| 1 | Alabama | 0 | 1 |
| 2 | Alabama | 0 | 2 |
| 3 | Alabama | 0 | 3 |
| 4 | Alabama | 0 | 4 |
| 5 | Alabama | 0 | 5 |
| 6 | Alabama | 0 | 6 |
+----+----------+-------------+------------+
The model that represents this table:
public class StateSurveyAssignment{
public long Id { get; set; }
public string State { get; set; }
public long RequestTypeId { get; set; }
public long SurveyId { get; set; }
}
And the code to seed the database in the Configuration.cs:
foreach (var state in Enum.GetValues(typeof(State))) {
foreach (var type in context.RequestTypes){
context.StateSurveyAssignments.AddOrUpdate(
ssa => ssa.Id,
new StateSurveyAssignment{
State = state.ToString(),
RequestTypeId = type.Id
}
);
}
}
My problem is that instead of updating/doing nothing to unchanged records, the seed method is duplicating each row. I've attempted to manually set the Id but had no luck.
EDIT:
This is what the database duplication looks like:
+----+----------+-------------+------------+
| Id | State | SurveyId | RequestType|
+----+----------+-------------+------------+
| 1 | Alabama | 0 | 1 |
| 2 | Alabama | 0 | 2 |
| 3 | Alabama | 0 | 3 |
| 4 | Alabama | 0 | 4 |
| 5 | Alabama | 0 | 5 |
| 6 | Alabama | 0 | 6 |
| ...| ... | ... | ... |
|307 | Alabama | 0 | 1 |
|308 | Alabama | 0 | 2 |
|309 | Alabama | 0 | 3 |
|310 | Alabama | 0 | 4 |
|311 | Alabama | 0 | 5 |
|312 | Alabama | 0 | 6 |
+----+----------+-------------+------------+
My Solution
I swear I'd tried setting my own Id at some point but tried it again per the answer and it seems to have worked. My final solution:
int counter = 1;
foreach (var state in Enum.GetValues(typeof(State))) {
foreach (var type in context.RequestTypes){
context.StateSurveyAssignments.AddOrUpdate(
ssa => ssa.Id,
new StateSurveyAssignment{
Id = counter,
State = state.ToString(),
RequestTypeId = type.Id
}
);
counter++;
}
}
The problem could be that the Id property in your StateSurveyAssignment class is an Identity column in the database.
This means that each row is not unique.
For example you try to insert the following several times using AddOrUpdate()
var model = new StateSurveyAssignment
{
State = "Alabama",
RequestTypeId = 1L,
SurveyId = 0L
};
Then each entry would have a different Id and thus you'll have duplicates.
I have 2 DataGridViews (DGVs).
theChipDGV will contain data like this (except with many more columns):
______________________________________________________________
| NAME | P/N | X | Y | ROTATION | PACKAGE |
|________|________|________|________|____________|___________|
| R16 | 147479 | 20.325 | 100.000| 0 | 0603 |
| C6 | 14739 | -5.325 | -10.105| 180 | 0603 |
| U45 | 123456 | 12.345 | 12.345 | 45 | 0402 |
|________|________|________|________|____________|___________|
theDataBaseDGV will contain data like this (except with many more columns):
____________________________________________________________________________________________
| PACKAGE | DESCRIPTION | FEEDER | VISION | SPEED | MACHINE | WIDTH | TIME |
|___________|_______________|__________|__________|_________|___________|_________|_______ |
| PLCC20 | N/A | 25MM | N/A | 3 | UNIVERSAL | 12MM | 0.05 |
| 0603 | 0603C_1.0 | 8X4 | 1 | 1 | FUJI-1 | 8MM | 20 |
| 0603 | 0603R_1.0 | 12X4 | 1 | 5 | FUJI-2 | 16MM | 0.20 |
|___________|_______________|__________|__________|_________|___________|_________|_______ |
What I would like to do is match the column in theChipDGV labeled PACKAGE with the same labeled column in theDataBaseDGV. If there is a match, the entire row will be concatted into a new DGV (let's label it: theFinalDGV). Also, if the PACKAGE type is matched and is also in the next line (like 0603) it will check to see if the column labeled Name in theChipDGV starts with a R or a C. Depending on which it starts with will determine the rest of the columns from theDataBaseDGV that will be used.
SO:
theFinalDGV will look like this:
_____________________________________________________________________________________________________________________________________________
| NAME | P/N | X | Y | ROTATION | PACKAGE | DESCRIPTION | FEEDER | VISION | SPEED | MACHINE | WIDTH | TIME |
|________|________|________|________|____________|___________|_______________|__________|__________|_________|___________|_________|________|
| R16 | 147479 | 20.325 | 100.000| 0 | 0603 | 0603R_1.0 | 12X4 | 1 | 5 | FUJI-2 | 16MM | 0.20 |
| C6 | 14739 | -5.325 | -10.105| 180 | 0603 | 0603C_1.0 | 8X4 | 1 | 1 | FUJI-1 | 8MM | 20 |
| U45 | 123456 | 12.345 | 12.345 | 45 | 0402 | | | | | | | |
|________|________|________|________|____________|___________|_______________|__________|__________|_________|___________|_________|________|
Notice, if there is no match it leaves the columns empty.
So:
Does anyone know how I can possibly go about doing this? I mostly would like to know how to match the values from 1 column with another and if there are multiple columns from theDataBaseDGV that have the same values.. then how to properly match those.
int chipRowCount = theChipDGV.RowCount;
int dataBaseRowCount = theDataBaseDGV.RowCount;
for (int count = 0; count < chipRowCount; count++)
{
for (int i = 0; i < dataBaseRowCount; i++)
{
if (theChipList[count].PkgStyle.Equals(theDataBaseList[i].PackageType) || (theChipList[count].PkgStyle.Contains(theDataBaseList[i].PackageType) &&
theDataBaseList[i].PartDescription.Contains("R") && theChipList[count].Name.StartsWith("R")) || ((theChipList[count].PkgStyle.Contains(theDataBaseList[i].PackageType) &&
theDataBaseList[i].PartDescription.Contains("C") && theChipList[count].Name.StartsWith("C"))))
{
if (!theChipList[count].Used)
{
theChipList[count].Used = true;
theFinalList.Add(new LoadLine(theChipList[count].Name, theChipList[count].PartNumber,
theChipList[count].XPlacement, theChipList[count].YPlacement, theChipList[count].Rotation,
theChipList[count].PkgStyle, theDataBaseList[i].PackageType, theDataBaseList[i].PartDescription,
theDataBaseList[i].Feeder, theDataBaseList[i].Vision, theDataBaseList[i].Speed,
theDataBaseList[i].Machine, theDataBaseList[i].TapeWidth, 0));
theFinalDGV.DataSource = theFinalList;
}
}
}
}
for (int count = 0; count < theChipList.Count; count++)
{
if (!theChipList[count].Used)
{
theFinalList.Add(new LoadLine(theChipList[count].Name, theChipList[count].PartNumber,
theChipList[count].XPlacement, theChipList[count].YPlacement, theChipList[count].Rotation,
theChipList[count].PkgStyle, string.Empty, string.Empty, string.Empty, string.Empty,
string.Empty, string.Empty, string.Empty, 0));
}
}