C#开发WinForm中怎么清空DataGridView控件绑定的数据

发布时间:2022-03-08 09:06:29 作者:iii
来源:亿速云 阅读:858

本文小编为大家详细介绍“C#开发WinForm中怎么清空DataGridView控件绑定的数据”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#开发WinForm中怎么清空DataGridView控件绑定的数据”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

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

1、设置DataSource为null

this.dgvDemo.DataSource = null

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

2、用DataGridView.Row.Clear()

this.dgvDemo.Rows.Clear()

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

C#开发WinForm中怎么清空DataGridView控件绑定的数据

以上两种方法都不是想要的结果。要想保持原有的列不被删除,就要清除原先绑定的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;
        }
    }
}

读到这里,这篇“C#开发WinForm中怎么清空DataGridView控件绑定的数据”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. [.NET开发] winform开发中使用datagridview遇到的问题
  2. 在WinForm中打印DataGridView操作代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

winform datagridview

上一篇:怎么用Python递归式实现二叉树前序,中序,后序遍历

下一篇:Pycharm中怎么更新第三方库

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》