如何使用Short Proc在datagridview中绑定数据 [英] How to bind data in datagridview using Short Proc

查看:76
本文介绍了如何使用Short Proc在datagridview中绑定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天

我想使用在Sql Server 2005中创建的存储过程在datagridview中显示表数据.

我的存储过程正在运行并正在执行.我的所有数据都从存储过程显示在Sql中.

这是我的C#代码:我已经在类中编写了一个方法.

Good day

I want to display my tables data in a datagridview using a Stored Procedure that I have created in Sql server 2005.

My Stored Procedure is working and executing. All my data is displaying in Sql from the Stored Procedure.

Here are my C# code: I have written a method inside of my class.

public string display()
       {
          SqlDatabase sqldb = new SqlDatabase(ConnectionString);
          IDataReader reader = sqldb.ExecuteReader("pSEL_mLearner", new Object[] { });


          return reader.ToString();
       }



这是我用来在显示"按钮上的主体中调用方法的代码.



And here is the code that I use to call my method in the main unit on my Display button.

private void btnDisplay_Click(object sender, EventArgs e)
       {
           dataGridView1.DataSource = login.display();



我的datagridview中没有显示任何数据.



No data is displayed inside my datagridview.

What is wrong?

推荐答案

public string display()




而不是返回字符串值


U可以返回DATASET,然后尝试...
希望它能起作用.




instead of return string value


U can return DATASET and Then Try...
I Hope It Will WOrk..



dataGridView1.DataSource = login.display()


写完上述行后,您忘记了将数据绑定到网格视图的功能,这就是为什么它不会将数据显示到网格视图的原因. 在此代码之后的那一行之后,您将在下面的行中插入.它将显示数据到网格视图.


After writting the above line you forgot the binding the data to grid view.Thats why It will not display the data to grid view.
At your this code after that line you insert below line.It will shows data to the grid view.

dataGridView1.DataBind()


这是我的问题的解决方法

Hi, here is the Solution to my question

public DataTable display()
       {

 SqlDatabase sqldb = new SqlDatabase(ConnectionString);
 DataTable dt = new DataTable();
 IDataReader reader = sqldb.ExecuteReader("pSEL_mLearner", new object[] { });

 dt.Load(reader);

return dt;
       }







private void btnDisplay_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = login.display();

                dataGridView1.DataSource = dt;
        }


这篇关于如何使用Short Proc在datagridview中绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆