使用数据库中的现有列填充gridview [英] Fill gridview with existing columns from database

查看:86
本文介绍了使用数据库中的现有列填充gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有以下问题:我有数据网格与网格编辑器创建的列。当我尝试从我的数据库中填写此字段时,在现有列之后填充信息。



我尝试过:



我尝试使用正确的数据填充网格列

Hi i have folowing problem: i have datagrid with columns created by grid editor. When i try to fill this fields from my database information is filled after the existing columns.

What I have tried:

I try to fill my grid columns with correct data

推荐答案

您可以让datagrid自动生成列。 />
You can let the datagrid autogenerate the columns.
SqlCommand cm = new SqlCommand("Select * from table1", con);
 SqlDataReader dr;
 dr = cm.ExecuteReader();
 DataTable dataTable = new DataTable();
 dataTable.Load(dr);
 dataGridView1.DataSource = dataTable;



这是一篇关于CodeProject的数据网格示例的优秀文章:详细的数据绑定教程 [ ^ ]


奇怪解决方案1不适合你,请从头开始重试,也许你已经设置了一些导致问题的属性。

这是我使用Windows身份验证的SQL Server Express的完整示例,它在我的带有SQL Server 2008 R2和.NET 4.5的Windows 7笔记本上没有任何问题。

Chinook数据库是一个示例数据库,可以从CodePlex下载:https://chinookdatabase.codeplex.com/

Strange that solution1 does not work for you, please try again from scratch, maybe you have set some properties that cause problems.
Here is my complete example for SQL Server Express with Windows Authentication, it works without any problems on my Windows 7 notebook with SQL Server 2008 R2 and .NET 4.5.
The Chinook database is an example database which can be downloaded from CodePlex: https://chinookdatabase.codeplex.com/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form6 : Form
    {
        string connectionString = @"Integrated Security=SSPI;Persist Security Info=False;Data Source=(local)\SQLEXPRESS;Initial Catalog=Chinook";
        DataTable dataTable;

        public Form6()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool result;

            using (var con = new SqlConnection(connectionString))
            {
                con.Open();
                result = con.State == System.Data.ConnectionState.Open;

                if (!result)
                {
                    MessageBox.Show("Could not connect !");
                }
                else
                {
                    Console.WriteLine("SQL Server Express " + con.ServerVersion);
                    SqlCommand cm = new SqlCommand("Select * from Artist", con);
                    SqlDataReader dr;
                    dr = cm.ExecuteReader();
                    dataTable = new DataTable();
                    dataTable.Load(dr);
                    dataGridView1.DataSource = dataTable;
                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dataTable.Rows.Count; i++)
            {
                string query = string.Empty;
                int result;

                switch (this.dataTable.Rows[i].RowState)
                {
                    case DataRowState.Added:
                        Debug.Print("Row " + i + "  state = added");
                        query = "INSERT INTO Artist (ArtistId, Name) VALUES(@ArtistId, @Name)";
                        break;
                    case DataRowState.Deleted:
                        Debug.Print("Row " + i + "  state = deleted");
                        break;
                    case DataRowState.Detached:
                        Debug.Print("Row " + i + "  state = detached");
                        break;
                    case DataRowState.Modified:
                        Debug.Print("Row " + i + "  state = modified");
                        query = "UPDATE Artist SET Name=@Name WHERE ArtistId=@ArtistId";
                        break;
                    //case DataRowState.Unchanged:
                    //    break;
                }

                // Execute parameterized query.
                if (!string.IsNullOrEmpty(query))
                {
                    using (var con = new SqlConnection(connectionString))
                    {
                        con.Open();

                        using (SqlCommand command = new SqlCommand(query, con))
                        {
                            Debug.Print("ArtistId = " + this.dataTable.Rows[i].ItemArray[0]);
                            Debug.Print("Name     = " + this.dataTable.Rows[i].ItemArray[1]);
                            command.Parameters.AddWithValue("@ArtistId", this.dataTable.Rows[i].ItemArray[0]);
                            command.Parameters.AddWithValue("@Name", this.dataTable.Rows[i].ItemArray[1]);
                            result = command.ExecuteNonQuery();
                            Debug.Print("result = " + result);
                        }
                    }
                }
            }
        }

    }
}


我找到了解决方案......我必须在加载数据之前清理datagrid
I find the solution ...I had to clean datagrid before load data
dataGridView1.Columns.Clear();


这篇关于使用数据库中的现有列填充gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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