如何将数据从第6行datagridview和只有5列复制到c#中的数据表 [英] How to copy the data from 6th row of datagridview and only 5 column into a datatable in c#

查看:54
本文介绍了如何将数据从第6行datagridview和只有5列复制到c#中的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview  control  grvListVehicle,它有10行和15列。但我只想将第5行和第5列的数据复制到数据表中。请帮助

I have a gridview  control grvListVehicle, it has 10 rows and 15 column . But I only want to copy data from 5th row and from 5 column into a data table. Please can you help

Pol

polachan

推荐答案

在这里,按钮1示例从数据源而不是DataGridView获取数据,而button2从DataGridView而不是数据源执行。

Here you go, button1 example for getting data from the data source rather than the DataGridView while button2 does it from the DataGridView rather than the data source.

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace ForumQuestion_cs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        BindingSource bsCustomers = new BindingSource();
        DataTable mCountryTable1;
        DataTable mCountryTable2;
        DataTable mCustomerDataTable2;
        private void Form1_Load(object sender, EventArgs e)
        {
            var ops = new Operations();

            // populate DataGridView via a Datatable, DataGridView
            // column names are the same names as the DataColumn names.
            // Of course you could load differently e.g. create columns
            // in the ide and set the DataPropertyName.
            bsCustomers.DataSource = ops.GetCustomers();
            dataGridView1.DataSource = bsCustomers;

            // DataTable we will copy to for BindingSource example
            mCountryTable1 = new DataTable();
            mCountryTable1.Columns.Add(new DataColumn()
            {
                ColumnName = "Country", DataType = typeof(string)
            });

            // DataTable we will copy to for raw DataGridView example
            mCountryTable2 = mCountryTable1.Clone();
            dataGridView3.DataSource = mCountryTable1;

            mCustomerDataTable2 = ((DataTable)bsCustomers.DataSource).Copy();
            dataGridView2.DataSource = mCustomerDataTable2;

            dataGridView4.DataSource = mCountryTable2;
        }
        /// <summary>
        /// Example via BindingSource
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (bsCustomers.Count >= 5)
            {
                var countryData =  ((DataTable)bsCustomers.DataSource).AsEnumerable()
                    .Skip(4).Select(row => row.Field<string>("Country"));

                foreach (var item in countryData)
                {
                    mCountryTable1.Rows.Add(item);
                }
            }
        }
        /// <summary>
        /// Example via DataGridView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView2.Rows.Count >= 5)
            {
                var countryData = dataGridView2.Rows.OfType<DataGridViewRow>()
                    .Skip(4).Select(row => Convert.ToString(row.Cells[4].Value));

                foreach (var item in countryData)
                {
                    mCountryTable2.Rows.Add(item);
                }
            }
        }
    }
}

获取数据

using System.Data;
using System.Data.SqlClient;

namespace ForumQuestion_cs
{
    class Operations
    {
        public string ExceptionMessage { get; set; }
        public bool HasException { get; set; }

        /// <summary>
        /// Replace with your SQL Server name
        /// </summary>
        private string Server = "KARENS-PC";
        /// <summary>
        /// Database in which data resides, see SQL_Script.sql
        /// </summary>
        private string Catalog = "NorthWindAzure";
        private string ConnectionString = "";
        public string ActualStatement { get; set; }
        public Operations()
        {
            ConnectionString =


" Data Source = {Server}; Initial Catalog = {Catalog}; Integrated Security =真英寸;
}
public DataTable GetCustomers()
{
var dt = new DataTable();

使用(SqlConnection cn = new SqlConnection {ConnectionString = ConnectionString})
{
using(SqlCommand cmd = new SqlCommand {Connection = cn})
{
cmd.CommandText =" SELECT CustomerIdentifier as Id,CompanyName as Company," +
" ContactName,City,Country,Phone FROM Customers" ;;
cn.Open();
dt.Load(cmd.ExecuteReader());
}
}

返回dt;
}
}
}
"Data Source={Server};Initial Catalog={Catalog};Integrated Security=True"; } public DataTable GetCustomers() { var dt = new DataTable(); using (SqlConnection cn = new SqlConnection { ConnectionString = ConnectionString }) { using (SqlCommand cmd = new SqlCommand { Connection = cn}) { cmd.CommandText = "SELECT CustomerIdentifier as Id, CompanyName as Company, " + "ContactName,City,Country,Phone FROM Customers"; cn.Open(); dt.Load(cmd.ExecuteReader()); } } return dt; } } }


这篇关于如何将数据从第6行datagridview和只有5列复制到c#中的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆