gridview中的多个数据

发布于 2024-11-09 10:10:38 字数 1841 浏览 0 评论 0原文

我遇到了一个新问题。

我想选择数据库中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆里有你的影子 2024-11-16 10:10:38

我能想到的两种可能的方法:

1:SQL级别(最好的一种,我不知道如何):我相信你可以在一个数据库调用中完成所有这些。可能是一个复杂的 SP,但 SQL 专家也许能够为您指明正确的方向。

2:C# 编码:您需要执行以下操作:

2a:创建一个普通的 C# 类,该类的属性映射到循环中最后一个查询返回的字段。即 Modelnr、Modelnaam 等。假设您将其命名为 MyModelClass。

public class MyModelClass{

public string Modelnr {get;set;} //set the type of property according to the datatype of DB field

public string Modelnaam{get;set;}
...other properties
}

2b:然后像这样修改你的代码:

List<MyModelClass> mmcList = new List<MyModelClass>();
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(); 
if(dr!=null && dr.HasRows){
MyModelClass mmcObj= new MyModelClass();
dr.Read();
mmcObj.Modelnr = dr["Modelnr"].ToString(); //Modelnr is String property in MyModelClass
mmcObj.Modelnaam= dr["Modelnaam"].ToString();//Modelnaam is a String prop in MyModelClass
...///so on other properties

mmcList.Add(mmcObj);
}
dr.Close();                 
teller++;
} 
gvModelAdmin.DataSource = mmcList; //set gv datasourc to the list on myModelClass
gvModelAdmin.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gvModelAdmin.DataBind(); 

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.

public class MyModelClass{

public string Modelnr {get;set;} //set the type of property according to the datatype of DB field

public string Modelnaam{get;set;}
...other properties
}

2b: Then modify your code like this:

List<MyModelClass> mmcList = new List<MyModelClass>();
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(); 
if(dr!=null && dr.HasRows){
MyModelClass mmcObj= new MyModelClass();
dr.Read();
mmcObj.Modelnr = dr["Modelnr"].ToString(); //Modelnr is String property in MyModelClass
mmcObj.Modelnaam= dr["Modelnaam"].ToString();//Modelnaam is a String prop in MyModelClass
...///so on other properties

mmcList.Add(mmcObj);
}
dr.Close();                 
teller++;
} 
gvModelAdmin.DataSource = mmcList; //set gv datasourc to the list on myModelClass
gvModelAdmin.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gvModelAdmin.DataBind(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文