Get the App
SLTechnology News&Howtos  ›  Development  › 

How to clear the data bound by DataGridView controls in WinForm Development with C #

Shulou Source: shulou.com Published: 2022-06-01 06:59:47 09月14日 Update

In this article, Xiaobian introduces in detail how to clean up the data bound by DataGridView controls in C# development WinForm, with detailed content, clear steps and proper handling of details. I hope that this article "how to clean up the data bound by DataGridView controls in C# development WinForm" can help you solve your doubts.

You sometimes need to clear the bound data after binding data using the DataGridView control, when clearing the DataGridView-bound data:

1. Set DataSource to null

This.dgvDemo.DataSource = null

Although the data bound by DataGridView can be cleared, the columns of DataGridView will also be deleted.

2. Use DataGridView.Row.Clear ()

This.dgvDemo.Rows.Clear ()

Using this method, an error will be reported, saying "this list cannot be cleared". The error message is as follows:

Neither of the above methods is the desired result. To keep the original column from being deleted, clear the data from the previously bound DataTable, and then rebind the DataTable

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

The sample code is as follows:

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 'project code', XIANGMUMC AS 'project name', DANJIA AS 'unit price', SHULIANG AS 'quantity' 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 will also delete the column of DataGridView / / this.dgvDemo.DataSource = null; / / will report an error: prompt "this list cannot be cleared" / / this.dgvDemo.Rows.Clear (); DataTable dt = this.dgvDemo.DataSource as DataTable Dt.Rows.Clear (); this.dgvDemo.DataSource = dt;} read here, this article "how to clear the data bound by DataGridView controls in C# development WinForm" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Data controls caching development articles code content methods projects reports tips appropriateness information unit price name ideas quantity new knowledge more steps Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Microsoft Linux Docker