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.
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 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;
}
I try to add new row to footer of column at calculated for Average of each cell in column.
Original datatable:
col1 | col2 | col3 | ... | col20 |
------|------|------|-----|-------|
5 | 10 | 5 | ... | 8 |
------|------|------|-----|-------|
8 | 7 | 8 | ... | 2 |
And I would like to do below :
col1 | col2 | col3 | ... | col20 |
------|------|------|-----|-------|
5 | 10 | 5 | ... | 8 |
------|------|------|-----|-------|
8 | 7 | 8 | ... | 2 |
-------|------|------|-----|-------|
6.5 | 8.5 | 6.5 | ... | 5 |
Here's my code:
if(sum[i] == 0 )
{
dt.Columns.Remove("BIN"+i.ToString()+"");
}
else
{
dt.Columns.Add("%BIN"+i.ToString()+"", typeof(decimal),"(BIN"+i.ToString()+" /
[In System])*100");
if (i == 1)
{
dt.Columns.Add("100-%BIN1", typeof(double), "100 -[%BIN1]");
Avg[i] = Convert.ToDouble(dt.Compute("Avg([col"+i.Tostring()+"])", string.Empty));
}
else
{
Avg[i] = Convert.ToDouble(dt.Compute("Avg([col" + i.ToString() + "])", string.Empty));
}
dt.Rows.Add("Avg_xbar"+i.ToString()+"", Avg[i]);
}
From my code above,There is a problem about result is below:
col1 | col2 | col3 | ... | col20 |
-----------|------|------|-----|-------|
5 | 10 | 5 | ... | 8 |
-----------|------|------|-----|-------|
8 | 7 | 8 | ... | 2 |
-----------|------|------|-----|-------|
Avg_xbar1 | 6.5 | | | |
-----------|------|------|-----|-------|
Avg_xbar2 | 8.5 | | | |
-----------|------|------|-----|-------|
Avg_xbar3 | 6.5 | | | |
-----------|------|------|-----|-------|
... | ... | | | |
-----------|------|------|-----|-------|
Avg_xbar20 | 5 | | | |
I'm assuming that you've got that code running in a loop for each column. You'll want to move the addition of rows outside of this loop.
for (var i = 0; i < columns.length; i++) {
// existing looping logic
}
dt.Rows.Add(Avg);
I have the below two tables
MBA
+--------+----------+------------+--------------+------------------+------------+
|mbaId | Channel | Product | ProgDate | ProgStartTime |ProgEndTime |
+--------+----------+ -----------+--------------+------------------+------------+
|12 | AA | SS | 01/04/2011 | 16:00:00 | 20:00:00 |
|13 | AA | SS | 01/04/2011 | 16:00:00 | 20:00:00 |
|14 | AA | SS | 01/04/2011 | 16:00:00 | 20:00:00 |
|15 | AA | SS | 01/04/2011 | 17:00:00 | 18:00:00 |
+--------+----------+------------+--------------+------------------+------------+
MAP
+----------+--------+---------------+-------------------+---------+----+
|mapId |Channel | Product |ProgDate | AdvTime| |
+----------+--------+---------------+-------------------+---------+----+
|8 | AA | SS | 01/04/2011 | 19:35:14| 30 |
|9 | AA | SS | 01/04/2011 | 18:40:19| 27 |
|10 | AA | SS | 01/04/2011 | 19:36:58| 35 |
|11 | AA | SS | 01/04/2011 | 17:47:13| 28 |
+----------+--------+---------------+-------------------+---------+----+
I would require the below output
+-----------+-------+-------+-------------+---------+--------------+-----------+-----+
| mapId |Channel|Product| ProgDate | AdvTime |ProgStartTime |ProgEndTime|mbaid|
+-----------+-------+-------+-------------+---------+--------------+-----------+-----+
| 8 | AA | SS | 01/04/2011 | 19:35:14|16:00:00 | 20:00:00 | 12 |
| 9 | AA | SS | 01/04/2011 | 18:40:19|16:00:00 | 20:00:00 | 13 |
| 10 | AA | SS | 01/04/2011 | 19:36:58|16:00:00 | 20:00:00 | 14 |
| 11 | AA | SS | 01/04/2011 | 17:47:13|16:00:00 | 17:00:00 | 15 |
+-----------+-------+-------+-------------+---------+--------------+-----------+-----+
I use the below code
select *
from MBA2 as mba
inner join Map2 as map on(map.Channel=mba.Channel and map.Product=mba.Product)
where(
(MBA.ProgStartTime < MBA.ProgEndTime
and MBA.ProgStartTime <=
case when datediff(mi, MBA.ProgStartTime, MBA.ProgEndTime) <= 60
then dateadd(mi, 5, Map.AdvTime)
else Map.AdvTime
end
and MBA.ProgEndTime >=
case when datediff(mi, MBA.ProgStartTime, MBA.ProgEndTime) <= 60
then dateadd(mi, -5, Map.AdvTime)
else Map.AdvTime
end) or
(MBA.ProgStartTime > MBA.ProgEndTime
and (MBA.ProgStartTime <=
case when 1440 - datediff(mi, MBA.ProgEndTime, MBA.ProgStartTime) <= 60
then dateadd(mi, 5, Map.AdvTime)
else Map.AdvTime
end
or MBA.ProgEndTime >=
case when 1440 - datediff(mi, MBA.ProgEndTime, MBA.ProgStartTime) <= 60
then dateadd(mi, -5, Map.AdvTime)
else Map.AdvTime
end)))
order by advtime asc
But i get duplicates i.e the value 19:35:14 matches the range 16:00:00 to 20:00:00 for the ids 12,13 and 14. I need one value in MAP match one value in MBA.
SO i used the below code to add data into a dataset and find the duplicates in MAPID
if (repeatID.Contains(int.Parse(dr["mapID"].ToString())))
{
duplicateID.Add(int.Parse(dr["mapID"].ToString()));
}
else
{
DataRow dr1 = dt.NewRow();
dr1[0] = int.Parse(dr["mapID"].ToString());
dr1[10] = int.Parse(dr["mbaID"].ToString());
dr1[1] = (dr["Channel"].ToString());
dr1[2] = (dr["Product"].ToString());
dr1[3] = (dr["ProgDate"].ToString());
dr1[4] = (dr["AdvTime"].ToString());
dr1[5] = (dr["Progstarttime"].ToString());
dr1[6] = (dr["Progendtime"].ToString());
dr1[7] = (dr["Channel"].ToString());
dr1[8] = (dr["Product"].ToString());
dr1[9] = (dr["ProgDate"].ToString());
dt.Rows.Add(dr1);
}
repeatID.Add(int.Parse(dr["mapID"].ToString()));
i = i + 1;
}
sCon.Close();
}
And remove the repeating id row. Then use the below query to display the data without duplicates and again to datatable. It returns without duplicate mapid in datatable but with MBAID Dulicates.
using (SqlConnection sCon = new SqlConnection(connec))
{
foreach (int id in duplicateID)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow ddr = dt.Rows[i];
if (ddr["mapID"].ToString() == id.ToString())
ddr.Delete();
}
sCon.Open();
{
SqlCommand cmd = new SqlCommand(#"select Distinct top 1 mp.Id as mapid, mb.Id as mbaid, mp.Channel, mp.Product, mp.ProgDate, mp.AdvTime, mb.Channel, mb.ProgStartTime,mb.ProgEndTime,mb.progdate, convert(time, dateadd(minute, datediff(minute, mb.progStartTime, mb.progEndTime), 0)) as timeDiff
from MBA22 as mb
inner join Map22 as mp on(mp.Channel=mb.Channel and mp.Product=mb.Product and mb.ProgDate=mp.ProgDate )
where(
(mb.ProgStartTime < mb.ProgEndTime
and mb.ProgStartTime <=
case when datediff(mi, mb.ProgStartTime, mb.ProgEndTime) <= 60
then dateadd(mi, 5, mp.AdvTime)
else mp.AdvTime
end
and mb.ProgEndTime >=
case when datediff(mi, mb.ProgStartTime, mb.ProgEndTime) <= 60
then dateadd(mi, -5, mp.AdvTime)
else mp.AdvTime
end) or
(mb.ProgStartTime > mb.ProgEndTime
and (mb.ProgStartTime <=
case when 1440 - datediff(mi, mb.ProgEndTime, mb.ProgStartTime) <= 60
then dateadd(mi, 5, mp.AdvTime)
else mp.AdvTime
end
or mb.ProgEndTime >=
case when 1440 - datediff(mi, mb.ProgEndTime, mb.ProgStartTime) <= 60
then dateadd(mi, -5, mp.AdvTime)
else mp.AdvTime
end)))
and mp.Id = '" + id + "' order by timeDiff asc", sCon);
cmd.CommandTimeout = 0;
SqlDataReader dr = cmd.ExecuteReader();
PLEASE HELPPPP
Try this query works accordingly but is risky as according to your need 1 record maps to another table so the sequence should be properly maintained else you will get wrong records
select
t2.mapId,
t1.Channel,
t1.Product,
t1.ProgDate,
t2.AdvTime,
t1.ProgStartTime,
t1.ProgEndTime,
t1.mbaid
FROM
(select
ROW_NUMBER() OVER(order by mbaid) as rId, *
from
mba
) t1,
(select
ROW_NUMBER() OVER(order by mapid) as rId, *
from
map
) t2
WHERE
t1.rId = t2.rId;a
FIDDLE
ROWNUMBER
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));
}
}