gridview中的多个数据
我遇到了一个新问题。
我想选择数据库中 statusID 高于当前用户的每个成员。 对于这些成员中的每一个,我希望在我的网格视图中显示填充的模型。
这意味着我必须对每个发现其 statusID 高于当前用户的成员重复 1 个 SELECT 查询。
为了填充我的网格视图,我使用 ASPX (C#) 背后的编程代码。据我所知,我一次只能在 gridview 中导入 1 个 SQL 查询。
有没有办法在 1 个 gridview 中显示所有这些记录?就像每个用户的 FE 一样,它只会将新数据添加到 gridview 而不是替换它?
这是我到目前为止得到的代码:
int statushelp = int.Parse(statusid);
SqlCommand cmd = new SqlCommand("SELECT COUNT (*) FROM Login WHERE Status_ID > " + statushelp + " ", con);
int aantal = (int)cmd.ExecuteScalar();
string[] onderdanen = new string[aantal];
int teller = 0;
SqlCommand cmd1 = new SqlCommand("SELECT User_ID FROM Login WHERE Status_ID > " + statushelp + " ", con);
dr = cmd1.ExecuteReader();
while (dr.Read())
{
onderdanen[teller] = dr["User_ID"].ToString();
teller++;
}
dr.Close();
string onderdaan;
teller = 0;
while (teller < onderdanen.Length)
{
onderdaan = onderdanen[teller].ToString();
SqlCommand cmd2 = new SqlCommand("SELECT M.Mod_ID AS Modelnr, M.Mod_Naam AS Modelnaam, M.Mod_Omschrijving AS Omschrijving, M.Taal_Id, M.User_ID FROM Toewijzing T, Model M WHERE T.User_ID = '" + onderdaan + "' AND T.Toe_Status = '" + "ja" + "' AND M.Mod_ID = T.Mod_ID", con);
dr = cmd2.ExecuteReader();
Debug.WriteLine(dr["M.Mod_ID"].ToString());
gvModelAdmin.DataSource = dr;
gvModelAdmin.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gvModelAdmin.DataBind();
dr.Close();
teller++;
}
真的欢迎任何帮助,因为我完全不知道如何做到这一点..
I ran into a new problem.
I want to select every member in my database with a statusID higher than the current user's.
For each of these members I want there filled in models to be showen in my gridview.
This means I'll have to repeat 1 SELECT query for each member found with a statusID higher than the current user's.
To fill my gridview I use programming code behind the ASPX (C#). So far as I know i can only inport 1 SQL query in my gridview at a time.
Is there any way to show all of these records in 1 gridview? Like F.E. for each user it will simply add the new data to the gridview in stead of replacing it ?
Here's the code I've got so far :
int statushelp = int.Parse(statusid);
SqlCommand cmd = new SqlCommand("SELECT COUNT (*) FROM Login WHERE Status_ID > " + statushelp + " ", con);
int aantal = (int)cmd.ExecuteScalar();
string[] onderdanen = new string[aantal];
int teller = 0;
SqlCommand cmd1 = new SqlCommand("SELECT User_ID FROM Login WHERE Status_ID > " + statushelp + " ", con);
dr = cmd1.ExecuteReader();
while (dr.Read())
{
onderdanen[teller] = dr["User_ID"].ToString();
teller++;
}
dr.Close();
string onderdaan;
teller = 0;
while (teller < onderdanen.Length)
{
onderdaan = onderdanen[teller].ToString();
SqlCommand cmd2 = new SqlCommand("SELECT M.Mod_ID AS Modelnr, M.Mod_Naam AS Modelnaam, M.Mod_Omschrijving AS Omschrijving, M.Taal_Id, M.User_ID FROM Toewijzing T, Model M WHERE T.User_ID = '" + onderdaan + "' AND T.Toe_Status = '" + "ja" + "' AND M.Mod_ID = T.Mod_ID", con);
dr = cmd2.ExecuteReader();
Debug.WriteLine(dr["M.Mod_ID"].ToString());
gvModelAdmin.DataSource = dr;
gvModelAdmin.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gvModelAdmin.DataBind();
dr.Close();
teller++;
}
Really any help is welcome couse I've got absolutely NO clue how to do this..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的两种可能的方法:
1:SQL级别(最好的一种,我不知道如何):我相信你可以在一个数据库调用中完成所有这些。可能是一个复杂的 SP,但 SQL 专家也许能够为您指明正确的方向。
2:C# 编码:您需要执行以下操作:
2a:创建一个普通的 C# 类,该类的属性映射到循环中最后一个查询返回的字段。即 Modelnr、Modelnaam 等。假设您将其命名为 MyModelClass。
2b:然后像这样修改你的代码:
Two possible ways I can think of:
1: SQL Level(would be best one and I don't know how):I believe you can do all this in one DB call. Could be a complex SP but a SQL expert may be able to point you in right direction.
2: C# Coding: Here's what you need to do:
2a: Create a plain C# class that has properties that maps to the fields returned from your last query in the loop. i.e. Modelnr,Modelnaam, etc. Say you named it MyModelClass.
2b: Then modify your code like this: