简体   繁体   English

如何根据 c# winforms 中的列日期和名称将 sql 中的数据输入特定的 datagridview 单元格

[英]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:数据库样本:
1

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的样子:
1

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.

相关问题 C# WinForms:如果按下某个键,我如何将特定字母写入 datagridview 单元格? - C# WinForms: How do I write a specific letter to a datagridview cell if a certain key is pressed? C# Winforms如何基于同一行上的另一个单元格从Datagridview获取文本到label? - C# Winforms how to get text from Datagridview to a label based on another cell on the same row? 如何在保存数据时屏蔽后从c#Winforms datagridview单元中检索密码 - How to retrieve password from c# Winforms datagridview cell after masking while saving the data 如何从 c# 中的 datagridview 通过列名从选定行中获取数据 - how do I get the data from selected row by its column name from datagridview in c# 在 C# 中,如何将数据库中的数据显示到 datagridview 中? - How do you display data from database into datagridview in C#? 如何将鼠标悬停在任何单元格上(在C#中)突出显示datagridview的行和/或列标签? - How do you highlight the row and/or column labels of a datagridview on mouseover of any cell (in c#)? 如何将数据从DataGridView保存到数据库? C#Winforms - How to save data from a DataGridView into database? c# winforms 使用WinForms在C#中从DataGridView保存数据 - Save data from DataGridView in C# with WinForms C# Winforms DatagridView 命名列中负单元格的条件颜色 - C# Winforms DatagridView conditional color for negative cell in named column 如何在datagridview中为特定列设置自定义日期格式? - C# - how to set custom date format for specific column in datagridview? - c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM