Datagrid和SQL 2005 [英] Datagrid and SQL 2005

查看:59
本文介绍了Datagrid和SQL 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我下面的代码工作正常.我的表中有三列(TypeID,Description,Rating).到目前为止,所有数据都已填充到我的数据网格中.

但是我需要的是我的数据网格应该只从我的表中提取2个库伦(TypeID和Description).如何更改代码?

请提供代码帮助.
谢谢.

我的代码:

Hi,
My code below is working fine. I have three column in my table( TypeID,Description,Rating). As of now all is populated in my datagrid.

But what I need is My datagrid should pull only 2 coulmn from my table( TypeID and Description). How to alter the code?

Please help with code.
Thanks.

My code:

namespace MovieProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void AutoradioButton1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Server=CHRIS-PC\\SQLEXPRESS;Database=MovieDesc;" +"Integrated Security=True");

            SqlCommand scmd = new SqlCommand("SELECT * FROM MovieType", conn);
            DataSet sds = new DataSet();
            conn.Open();

            SqlDataAdapter sadptr = new SqlDataAdapter(scmd);
            sadptr.Fill(sds, "MovieType");

            if (sds.Tables["MovieType"].Rows.Count == 0)
            {
                MessageBox.Show("There are no DB.");
            }
            else
            {
                this.listBox1.DataSource = sds.Tables["MovieType"];
                this.listBox1.DisplayMember = "Description";
            }

            checkedListBox1.DataSource = sds.Tables["MovieType"];
            checkedListBox1.DisplayMember = "Rating";
            dataGridView1.DataSource = sds.Tables[0];
          }
        }
    }

推荐答案

SQL"Select"语句使用通配符*,因此返回表中的所有列.

更改您的SQL SELECT语句以完全选择所需的列.

所以

选择* FROM MovieType

变为:

SELECT TypeID,说明来自MovieType

您还需要将显示成员从评分"更改为要返回的列之一.

就是这样.
The SQL "Select" statement uses the wildcard, * and so returns all columns in the table.

Change your SQL SELECT statement to select exactly the columns you want.

so

SELECT * FROM MovieType

becomes:

SELECT TypeID, Description FROM MovieType

You also need to change your display member from "Rating" to one of the columns that is being returned.

That should be it.


尝试这个
DataTable dt1 = sds.Tables[0];
dt1.Columns.Remove(dt1.Columns[0]);
dt1.AcceptChanges();
dataGridView1.DataSource = dt1;


或自己构建DataGrid,全选,然后仅显示所需的列,尽管实际上是最佳做法,但是,减少的数据量=减少的重载等
or build your DataGrid yourself, select all and then show only the columns you want, best practice to do the above though really, less data pulled = less overload etc etc


这篇关于Datagrid和SQL 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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