Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report