C#Windows窗体/基础数据网格 [英] c# windows forms / basic data grid

查看:179
本文介绍了C#Windows窗体/基础数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个我用C#是,似乎是我无法找到正确的答案最基本的问题,这么多的信息在网上。

One of the problems I am having with c# is that there seems to be so much information online that I am having trouble finding the right answer to the most basic of questions.

我试图做一些简单:
我有一个按钮,我点击它,它查询数据库并填充我的Windows窗体上的数据网格

I am trying to do something simple: I have a button, I click it, it queries the database and populates a datagrid on my windows form.

    private void button1_Click(object sender, EventArgs e)
    { 
        SqlConnection c = new SqlConnection("Data Source = (local); Integrated Security = true; Initial Catalog = pubs;  "); 

        c.Open();
        // 2
        // Create new DataAdapter
        SqlCommand cmd = c.CreateCommand();
        cmd.CommandText = @" SELECT * FROM Authors ";
        SqlDataReader reader = cmd.ExecuteReader();

        dataGridView1.DataSource = reader;
        dataGridView1.DataBind();
    }



错误1'System.Windows.Forms.DataGridView'不包含一个定义对于'的DataBind',没有扩展方法'的DataBind'接受的类型'System.Windows.Forms.DataGridView'的第一个参数可以发现.....

Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'DataBind' and no extension method 'DataBind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found.....

我我可能缺少使用指令但哪一个?多谷歌搜索告诉我如何在雅虎RSS订阅绑定到一个gridview上的使用指令提供各种不起眼的细节。

I am probably missing a "using directive" but which one? Multiple Google searches tell me how to bind a Yahoo RSS Feed to a gridview or provide various obscure details on "using directives".

也许我现在用的是SqlDataReader的不正确。我应该使用SqlAdapter呢?发生了什么事都好基本教程在线为Windows的C#形式?几个月前,我发现一对夫妇伟大的教程,但他们似乎已经失去了他们pageranking并再使用基本的谷歌搜索,我找不到。

Maybe I am using the SqlDataReader incorrectly. Should I be using SqlAdapter instead? What happened to all the good basic tutorials online for windows c# forms? A few months ago I found a couple great tutorials, but they seem to have lost their pageranking and I cannot find them anymore using basic google searches.

推荐答案

试试这个:

using (SqlConnection conn = new SqlConnection("your connection string"))
{
    conn.Open();
    using (SqlCommand cmd = new SqlCommand(@"SELECT * FROM Authors", conn))
    {
        using (SqlDataAdapter adap = new SqlDataAdapter(cmd))
        {
            DataTable dt = new DataTable();
            adap.Fill(dt);
            dataGridView1.DataSource = dt;
        }
    }
}



在DataGridView不具有的DataBind()方法,因为它并不需要一个。设定DataSource属性为您处理绑定。该使用()块将自动关闭,并为你的一切,以及处置

The DataGridView does not have a DataBind() method because it doesn't need one. Setting the DataSource property handles the binding for you. The using() blocks will automatically close and dispose of everything for you as well.

注意:你应该将连接字符串与一个有效的连接字符串。我离开你的我的样本,以避免水平滚动条,而我不知道你是有效的反正。当您运行使用您的连接字符串中的代码,你可能会得到一个运行时错误。 www.connectionstrings.com 是搞清楚一个有效的连接字符串一个很好的资源。

Note: you should replace "your connection string" with a valid connection string. I left yours out of my sample to avoid the horizontal scrollbars, and I'm not sure yours is valid anyway. You may get a runtime error when you run the code using your connection string. www.connectionstrings.com is a great resource for figuring out a valid connection string.

更新:而不是使用(套)块,你也可以做这样的:

Update: instead of the nested using() blocks, you can also do it like this:

using (SqlConnection conn = new SqlConnection("..."))
using (SqlCommand cmd = new SqlCommand(@" SELECT * FROM Authors", conn))
using (SqlDataAdapter adap = new SqlDataAdapter(cmd))
{
    conn.Open();
    DataTable dt = new DataTable();
    adap.Fill(dt);
    dataGridView1.DataSource = dt;
}



我喜欢嵌套的风格,但它的后者的一半,六十来个其他的给我。通常情况下,我会封装这样的代码放到一个类(称为DataGetter或其他)像一个静态方法:

I prefer the nested style, but it's "half of one, six dozen of the other" to me. Typically, I would encapsulate code like this into a class (called "DataGetter" or whatever) with a static method like:

public static DataTable GetData(string query)
{
    // do all the connecting and adapting and filling and so forth
}

让你按一下按钮的代码将是简单的:

so that the code in your button click would be as simple as:

dataGridView1.DataSource = DataGetter.GetData("SELECT * FROM AUTHORS");



不过,我的的在任何性能关键部分做我的代码,因为你有时要保持一个SqlCommand对象(和它的SqlParameter集合)调用之间。你做的的需要不断的SqlConnection电话之间物体周围,由于连接池(实际上,你不想让他们在任何情况下左右)。

However, I would not do this in any performance-critical section of my code, since you sometimes want to keep a SqlCommand object (and its SqlParameter collection) around between calls. You do not need to keep SqlConnection objects around between calls, thanks to connection pooling (in fact, you don't want to keep them around under any circumstances).

这篇关于C#Windows窗体/基础数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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