zoukankan      html  css  js  c++  java
  • DataGridView:DataGridView控件清空绑定的数据

    使用DataGridView控件绑定数据后有时需要清空绑定的数据,在清除DataGridView绑定的数据时:

    1、设置DataSource为null

    this.dgvDemo.DataSource = null
    

    这样虽然可以清空DataGridView绑定的数据,但是DataGridView的列也会被删掉。

    2、用DataGridView.Row.Clear()

    this.dgvDemo.Rows.Clear()
    

    使用这种方法会报错,提示“不能清除此列表”,报错信息如下:

    以上两种方法都不是想要的结果。要想保持原有的列不被删除,就要清除原先绑定的DataTable中的数据,然后重新绑定DataTable

    DataTable dt = this.dgvDemo.DataSource as DataTable;
    dt.Rows.Clear();
    this.dgvDemo.DataSource = dt;
    

    示例代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace DataGridViewDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            string strCon = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
    
            private void btn_BindingData_Click(object sender, EventArgs e)
            {
                DataTable dt = GetDataSource();
                this.dgvDemo.DataSource = dt;
            }
    
            private DataTable GetDataSource()
            {
                DataTable dt = new DataTable();
                SqlConnection conn = new SqlConnection(strCon);
                string strSQL = "SELECT XIANGMUCDDM AS '项目代码',XIANGMUMC AS '项目名称', DANJIA AS '单价',SHULIANG AS '数量' FROM InPatientBillDt WHERE 就诊ID='225600'";
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                try
                {
                    conn.Open();
                    adapter.Fill(dt);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
                return dt;
            }
    
            private void btn_Clear_Click(object sender, EventArgs e)
            {
                // this.dgvDemo.DataSource = null会将DataGridView的列也删掉
                //this.dgvDemo.DataSource = null;
    
                // 会报错:提示“不能清除此列表”
                //this.dgvDemo.Rows.Clear();
    
                DataTable dt = this.dgvDemo.DataSource as DataTable;
                dt.Rows.Clear();
                this.dgvDemo.DataSource = dt;
            }
        }
    }
    

    示例程序下载地址:https://pan.baidu.com/s/1brhiWKB

  • 相关阅读:
    【Solr】新建core后,启动服务访问web报错 HTTP Status 503
    【Tomcat】tomcat报连接超时错误
    【Tomcat】tomcat报错 removeGeneratedClassFiles failed
    【MongoDB】MongoDb的“not master and slaveok=false”错误及解决方法
    【Solr】 solr对拼音搜索和拼音首字母搜索的支持
    【Junit】The import org.junit.Test conflicts with a type defined in the same file报错
    maven的pom.xml文件报错问题
    ubuntu卸载virtualbox
    ClassNotFoundException: INameEnvironment
    详解为什么32位系统只能用4G内存.
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/8464784.html
Copyright © 2011-2022 走看看