[英]How do you input data from sql into a specific datagridview cell based on column date and name in c# winforms
I have a table in my database that stores data entry.我的数据库中有一个存储数据条目的表。 theres a name column where each persons name is inserted as they scan a serial number, 3 middle columns for serial entry and the last column is the date.
有一个姓名列,每个人在扫描序列号时都会插入姓名,中间有 3 列用于序列输入,最后一列是日期。 database sample:
数据库样本:
i have a winform with a datagridview that populates the dates in the column header for every day of the month.我有一个带有 datagridview 的 winform,它在 header 列中填充每月每一天的日期。 what i am trying to figure out is how to populate the distinct name along with the count of their serial numbers based on what day it is in the datagridview.
我想弄清楚的是如何根据数据网格视图中的日期填充不同的名称以及它们的序列号计数。 i can populate the names but the serial counts on specific days is getting me.
我可以填充名称,但特定日期的连续计数让我很着迷。 Here is what my winform looks like:
这是我的winform的样子:
and the code:和代码:
DateTime dt = DateTime.Now;
var startDate = new DateTime(dt.Year, dt.Month, 1);
int days = DateTime.DaysInMonth(dt.Year, dt.Month);
for (int i = 0; i < days; i++)
{
dataGridView1.Columns.Add("clm_" + startDate.ToShortDateString(), startDate.ToString("dd-MMM"));
startDate = startDate.AddDays(1);
}
using (SqlConnection sqlConnection = new SqlConnection(@"connection"))
{
SqlCommand sqlCmd = new SqlCommand("SELECT DISTINCT NAME FROM repair_data_entry", sqlConnection);
sqlConnection.Open();
SqlDataReader sqlReaderName = sqlCmd.ExecuteReader();
while (sqlReaderName.Read())
{
dataGridView1.Rows.Add(sqlReaderName["NAME"].ToString());
}
sqlReaderName.Close();
}
what i need to do is pull the serial count per day per tech.我需要做的是每项技术每天提取序列数。 like Bill Berrys serial count for a specific day shows up in that specific cell.
就像比尔贝瑞斯特定日期的序列计数显示在该特定单元格中。 it may be a thing where i need to set the database up differently, i don't know.
这可能是我需要以不同方式设置数据库的事情,我不知道。 and that wouldn't be a problem.
这不会是一个问题。 i should note there aren't specific names either.
我应该注意也没有具体的名字。 there's going to be 20-30 names that change on a daily basis so my intent was something dynamic.
每天都会有 20-30 个名字在变化,所以我的意图是动态的。
EDIT:编辑:
I've been fiddling with SQL fiddler and I found the query that works, However.我一直在摆弄 SQL 提琴手,但我发现查询有效。 I had to hard code the dates?
我必须硬编码日期? How would I make them dynamic based on what's in the CREATED column since techs will be entering in data on a daily basis?
由于技术人员每天都会输入数据,我如何根据 CREATED 列中的内容使它们动态化?
Here's the query:这是查询:
SELECT NAME AS TECH,
[12/1/2020], [12/2/2020]
FROM
(SELECT NAME, REP, CREATED
FROM repair_data_entry) AS SourceTable
PIVOT
(
COUNT(REP)
FOR CREATED IN ([12/1/2020], [12/2/2020])
) AS PivotTable;
http://sqlfiddle.com/#!18/8bc96/1 http://sqlfiddle.com/#!18/8bc96/1
I figured out the sql Query:我想出了 sql 查询:
DECLARE @Date AS VARCHAR(MAX),
@query AS VARCHAR(MAX)
select @dATE = STUFF((SELECT ',' + QUOTENAME(REPLACE(CONVERT(VARCHAR(10), CREATED, 101), '-', '/'))
from repair_data_entry
group by CREATED
ORDER BY CREATED
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')
SET @query = 'SELECT NAME AS TECH, ' + @Date + '
FROM
(SELECT NAME, CREATED, REP
FROM repair_data_entry) AS S
PIVOT
(
COUNT(REP)
FOR CREATED IN ('+@Date+')
) P'
EXECUTE(@query)
This does exactly what I want.这正是我想要的。 so that answers that.
所以这就回答了这个问题。 sqlFiddle
sqlFiddle
can anyone point me in the direction to change the date style to "MM / dd / yyyy"?谁能指出我将日期样式更改为“MM / dd / yyyy”的方向?
EDIT: found the answer.编辑:找到答案。 updated query
更新的查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.