如何将存储过程中的数据直接添加到C#中的DataGridView中?

时间:2021-09-29 16:37:14

I wanted to know how I can add data returned from a stored procedure directly into a dataGridview.

我想知道如何将存储过程返回的数据直接添加到dataGridview中。

I'm creating an attendance system in C# and I was sending the Class_Id and Sub_Id back to the stored procedure via combobox so that I can I can only see the students that are present in a certain class for a certain subject but I don't know how to add the returned results directly into a dataGridView

我正在用C#创建一个考勤系统,我正在通过组合框将Class_Id和Sub_Id发送回存储过程,这样我就可以只看到特定科目中某些班级的学生,但我不能知道如何将返回的结果直接添加到dataGridView中

Attendance Screenshot.

In the screenshot, I am sending back the Class_Id and Sub_Id so that It only shows me the students that have those id in the stored procedure.

在屏幕截图中,我发送回Class_Id和Sub_Id,以便它只向我显示存储过程中具有这些id的学生。

I executed the query in the stored procedure and It showed me the students that had the Class_id and Sub_Id I specified.

我在存储过程中执行了查询,它向我展示了我指定了Class_id和Sub_Id的学生。

Stored procedure

Here is the screenshot for the code

这是代码的屏幕截图

Code Image

2 个解决方案

#1


2  

Use an Adapter to fill the DataTable and add that DataTable to the DataSource property of the DataGridView:

使用适配器填充DataTable并将该DataTable添加到DataGridView的DataSource属性:

DataTable result = new DataTable();
using (SqlConnection conn = new SqlConnection(ConnectionString))
 {
   SqlDataAdapter adapter = new SqlDataAdapter("StoredProcedureName", conn);
   adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
   adapter.SelectCommand.Parameters.Add("@subid",SqlDbType.Int).Value= comboBox1.SelectedValue;
   adapter.SelectedCommand.Parameters.Add("@classid",SqlDbType.Int).Value=comboBox1.SelectedValue;
   adapter.Fill(result);
 }
 dataGridView1.DataSource = result;

If your @subid and @classid is typeof int, I think you need to convert your ComboBox value to int. You can try with Convert.ToInt32(comboBox1.Value) or even better int.TryParse(comboBox1.Value, out result)

如果你的@subid和@classid是typeof int,我认为你需要将你的ComboBox值转换为int。你可以试试Convert.ToInt32(comboBox1.Value)甚至更好的int.TryParse(comboBox1.Value,out result)

#2


0  

may be this can be helpful to you.. you can replace my query with your stored procedure

可能这对你有帮助..你可以用你的存储过程替换我的查询

    Dim con As SqlConnection = New SqlConnection("server = ADITYA\CBOWR; database = adi; integrated security = true;")
    Dim cmd As SqlCommand = New SqlCommand()
    Dim query As String = "Select * from address"
    cmd.Connection = con
    cmd.CommandType = CommandType.Text
    cmd.CommandText = query
    Dim sda As SqlDataAdapter = New SqlDataAdapter(query, con)
    Dim ds As DataSet = New DataSet()
    con.Open()
    sda.Fill(ds)
    GridView1.DataSource = ds
    GridView1.DataBind()
    con.Close()

#1


2  

Use an Adapter to fill the DataTable and add that DataTable to the DataSource property of the DataGridView:

使用适配器填充DataTable并将该DataTable添加到DataGridView的DataSource属性:

DataTable result = new DataTable();
using (SqlConnection conn = new SqlConnection(ConnectionString))
 {
   SqlDataAdapter adapter = new SqlDataAdapter("StoredProcedureName", conn);
   adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
   adapter.SelectCommand.Parameters.Add("@subid",SqlDbType.Int).Value= comboBox1.SelectedValue;
   adapter.SelectedCommand.Parameters.Add("@classid",SqlDbType.Int).Value=comboBox1.SelectedValue;
   adapter.Fill(result);
 }
 dataGridView1.DataSource = result;

If your @subid and @classid is typeof int, I think you need to convert your ComboBox value to int. You can try with Convert.ToInt32(comboBox1.Value) or even better int.TryParse(comboBox1.Value, out result)

如果你的@subid和@classid是typeof int,我认为你需要将你的ComboBox值转换为int。你可以试试Convert.ToInt32(comboBox1.Value)甚至更好的int.TryParse(comboBox1.Value,out result)

#2


0  

may be this can be helpful to you.. you can replace my query with your stored procedure

可能这对你有帮助..你可以用你的存储过程替换我的查询

    Dim con As SqlConnection = New SqlConnection("server = ADITYA\CBOWR; database = adi; integrated security = true;")
    Dim cmd As SqlCommand = New SqlCommand()
    Dim query As String = "Select * from address"
    cmd.Connection = con
    cmd.CommandType = CommandType.Text
    cmd.CommandText = query
    Dim sda As SqlDataAdapter = New SqlDataAdapter(query, con)
    Dim ds As DataSet = New DataSet()
    con.Open()
    sda.Fill(ds)
    GridView1.DataSource = ds
    GridView1.DataBind()
    con.Close()