真爱无限的知识驿站

学习积累技术经验,提升自身能力

将DataGridViewRows转换为DataTable数据


.Net WinForm代码:

        /// <summary>
        /// 将DataGridViewRows转换为DataTable数据
        /// </summary>
        /// <param name="Listrows">DataGridView的行集合</param>
        /// <param name="dtSource">源数据表</param>
        /// <param name="colum">映射关系</param>
        /// <param name="dtDestination">目的数据表</param>
        /// <returns>返回新的DataTable</returns>
        public static DataTable Getdatatable(DataGridViewRowCollection Listrows, DataTable dtSource, string[,] colum, DataTable dtDestination)
        {
            //  string[,] colum = { { "id1", "id2" }, { "name1", "name2" } };  //colum是源DataGridViewRow到目标DataRow的映射关系
            DataTable dtNew = dtSource.Clone();//复制表结构
            foreach (DataGridViewRow dgvr in Listrows)
            {
                DataRow dataRow = (dgvr.DataBoundItem as DataRowView).Row;  //转换成数据行DataRow
                dtNew.ImportRow(dataRow);  //添加DataRow到DataTable
            }
            string[] copy = new string[colum.Length / 2];
            for (int i = 0; i < colum.Length / 2; i++)
            {
                copy[i] = colum[i, 0];  //获取源数据要的列
            }
            DataTable dtNew2 = dtNew.DefaultView.ToTable(false, copy);//复制源表结构和数据,并只保留copy数据中的列
            if (dtDestination != null)
            {
                for (int j = 0; j < dtNew2.Rows.Count; j++)
                {
                    DataRow row = dtDestination.NewRow();
                    for (int f = 0; f < colum.Length / 2; f++)
                    {
                        row[colum[f, 1]] = dtNew2.Rows[j][colum[f, 0]]; //将源表中每行的各列数据复制到对应的目的表的行中
                    }
                    dtDestination.Rows.Add(row); //添加到目的表
                }
            }
            return dtDestination;
        }


c#winform中,对DataGridView数据进行操作,一次性保存

需求:不能每加一条数据就操作数据库,要完成所有的数据加入界面,点击保存时才一次性保存。

[转载]C#多线程BackgroundWorker使用示例

.Net WinForm Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;////
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace BackgroundWorkerSample
{
    public partial class CalculateAddForm : Form
    {
        protected BackgroundWorker backgroundWorker1 = new BackgroundWorker();
        //private int numberToCompute = 0;
        //private int highestPercentageReached = 0;
        public CalculateAddForm()
        {
            InitializeComponent();
            InitializeBackgoundWorker();
           
        }
        // Set up the BackgroundWorker object by
        // attaching event handlers.
        private void InitializeBackgoundWorker()
        {
            this.backgroundWorker1.WorkerReportsProgress = true;
            this.backgroundWorker1.WorkerSupportsCancellation = true;
           
            this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        }
        private long ComputeAdd(int n, BackgroundWorker worker, DoWorkEventArgs e)


c#/.net用txt、in等作为数据存储的配置类


using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Text.RegularExpressions;


namespace Tools

{

    #region 配置文件类 ConfigFile

Excel文件与DataSet之间的转化的探索与实现

.net操作Excel的支持库下载 NPOI2.0.1.zip

.Net Json转换为DataTable小数自动转成整数造成错误的问题

1、原因

原生Json反序列化时,如果多行数据第一行为int,后面多行为decimal会字段转化为int

C#winform中只允许程序运行一个实例、显示已经运行的界面

private static System.Threading.Mutex mutex;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
...

.Net中Remoting通信的应用,有发送和返回信息

界面如图:  代码:服务端using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;u

用xml操作代码配置Web.config文件

    public class Webconfig    {        private string XmlFilePath;        private XmlDocument xmldoc

c#/.net文件配置通用类

    public class ConfigFile : IConfig
    {
        // Fields
        private string _fileName;
        private static ConfigFile _Instance;

...

<< < 4 5 6 7 8 9 10 11 12 13 > >>

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号