如何使用数据库动态生成数据网格? [英] How do I dynamically generate datagrid using database?

查看:85
本文介绍了如何使用数据库动态生成数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最终结果处理系统。我必须制作gridview,其中课程代码将动态生成。



提前致谢。

解决方案

 DataGridView可以在绑定模式,未绑定模式和虚拟模式下显示数据。绑定模式适用于使用与数据存储的自动交互来管理数据。 DataGridView控件的一个非常常见的用途是绑定到数据库中的表。未绑定模式适用于显示以编程方式管理的相对少量的数据。虚拟模式允许您等到实际显示单元格以提供其将包含的值,从而为您提供更高程度的控制。 

以下C#源代码说明了如何将DataGridView连接到数据库以及如何从DataGridView中新建/更新或删除数据库值。



< pre lang =c#> 使用系统;
使用 System.Data;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;

命名空间 WindowsFormsApplication1
{
public partial class Form1:Form
{
SqlCommand sCommand;
SqlDataAdapter sAdapter;
SqlCommandBuilder sBuilder;
DataSet sDs;
DataTable sTable;

public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
string connectionString = Data Source = .; Initial Catalog = pubs; Integrated Security = True;
string sql = SELECT * FROM Stores ;
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
sCommand = new SqlCommand(sql,connection);
sAdapter = new SqlDataAdapter(sCommand);
sBuilder = new SqlCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, Stores);
sTable = sDs.Tables [ Stores];
connection.Close();
dataGridView1.DataSource = sDs.Tables [ Stores];
dataGridView1.ReadOnly = true ;
save_btn.Enabled = false ;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}

private void new_btn_Click( object sender,EventArgs e)
{
dataGridView1.ReadOnly = false ;
save_btn.Enabled = true ;
new_btn.Enabled = false ;
delete_btn.Enabled = false ;
}

private void delete_btn_Click( object sender,EventArgs e)
{
if (MessageBox.Show( 是否要删除此行? 删除,MessageBoxButtons.YesNo)== DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows [ 0 ]指数);
sAdapter.Update(sTable);
}
}

私有 void save_btn_Click( object sender,EventArgs e)
{
sAdapter.Update(sTable);
dataGridView1.ReadOnly = true ;
save_btn.Enabled = false ;
new_btn.Enabled = true ;
delete_btn.Enabled = true ;
}
}
}


I am working with final result processing system. I have to make gridview where course code will be generate dynamically.

Thanks in advance.

解决方案

The DataGridView can display data in Bound mode, unbound mode and Virtual mode . Bound mode is suitable for managing data using automatic interaction with the data store. One very common use of the DataGridView control is binding to a table in a database. Unbound mode is suitable for displaying relatively small amounts of data that you manage programmatically. Virtual mode gives you a higher degree of control by allowing you to wait until a cell is actually being displayed to provide the value it will contain.

The following C# source code illustrate how to connect a DataGridView to a database and new/update or delete the database values from DataGridView.


using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SqlCommand sCommand;
        SqlDataAdapter sAdapter;
        SqlCommandBuilder sBuilder;
        DataSet sDs;
        DataTable sTable;        

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";
            string sql = "SELECT * FROM Stores";
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            sCommand = new SqlCommand(sql, connection);
            sAdapter = new SqlDataAdapter(sCommand);
            sBuilder = new SqlCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "Stores");
            sTable = sDs.Tables["Stores"];
            connection.Close();
            dataGridView1.DataSource = sDs.Tables["Stores"];
            dataGridView1.ReadOnly = true;
            save_btn.Enabled = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void new_btn_Click(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = false;
            save_btn.Enabled = true;
            new_btn.Enabled = false;
            delete_btn.Enabled = false;
        }

        private void delete_btn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                sAdapter.Update(sTable);
            }
        }

        private void save_btn_Click(object sender, EventArgs e)
        {
            sAdapter.Update(sTable);
            dataGridView1.ReadOnly = true;
            save_btn.Enabled = false;
            new_btn.Enabled = true;
            delete_btn.Enabled = true;
        }
    }
}


这篇关于如何使用数据库动态生成数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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