如何将内容从一个单元复制到另一个 [英] How to copy the content from one cell to another

查看:95
本文介绍了如何将内容从一个单元复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查是否有任何column1,任何单元格都不为空。我想先清空然后将文件复制到下一个列单元格。我的想法是检查一个特定的Column1-所有单元格是否都说 COLUMN1,其中一个单元格不为空。然后,我需要文件[我已将文件路径附加到该特定单元格]复制到下一个column2。同时我想将文件复制到桌面上的文件夹中。假设位置为C:/ user / elec / copy,并且我想删除Column1 -cell数据。

I am try to check if any column1, any of the cell's is not empty. i want to make then empty and copy the file to next column-cell. What ,my idea is to check if one particular Column1- all cells lets say "COLUMN1" one of the cell is not empty. Then i need the file [ I have attached the file path to that particular cell] to get copied to next column2. at the same time i want to copy the file to a folder on my desktop Lets say the location is C:/user/elec/copy, and i want to erase the Column1 -cell data.

我该怎么做。

已编辑编码.....

 private void button6_Click(object sender, EventArgs e)
    {
            string copyPath = @"C:\user\elec\copy";
            if (!System.IO.Directory.Exists(copyPath))
                System.IO.Directory.CreateDirectory(copyPath);
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[2].Value != null && !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[2].Value.ToString()))
                {
                    string filePath = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    if (System.IO.File.Exists(filePath))
                    {
                        string fileName = System.IO.Path.GetFileName(filePath);
                        string newpath = System.IO.Path.Combine(copyPath, fileName);
                        System.IO.File.Copy(filePath, newpath, true);
                        dataGridView1.Rows[i].Cells[3].Value = newpath;

                        try
                        {
                            SqlConnection con = new SqlConnection(@"Data Source=SREEJITHMOHA492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
                            {
                                con.Open();
                                SqlCommand cmd = con.CreateCommand();
                                cmd.CommandText = "update cncinfo set draftpath=@draftpath,releasepath=@releasepath Where part=@part";
                                cmd.Parameters.Add("@draftpath", SqlDbType.NVarChar).Value = filePath;
                                cmd.Parameters.Add("@releasepath", SqlDbType.NVarChar).Value = newpath;
                                cmd.CommandText = "update cncinfo set draftpath='" + string.Empty + "',releasepath=@releasepath Where part=@part";
                                //you must have the id value in datagridview to update the specific record.
                                cmd.Parameters.Add("@part", SqlDbType.NVarChar).Value = Convert.ToString(dataGridView1.Rows[i].Cells["Part Number"].Value);
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    dataGridView1.Rows[i].Cells[2].Value = string.Empty;
                }
            }
    }


推荐答案

您的代码中有很多问题。您正在尝试读取 DataGridViewCell 而不是其值。要读取单元格值,必须必须从 DataGridVIewCell .Value 属性返回值。即。 string x = dgv.Rows [0] .Cells [1] .Value 。行不包含列,它包含单元格。因此,要从特定单元格读取值,应从Cell对象而不是Column读取该值。

There are lot of problems in your code. you are trying to read DataGridViewCell instead of its value. to read the cell value you must have to return value from .Value property of DataGridVIewCell. ie. string x=dgv.Rows[0].Cells[1].Value. Row does not contains Columns it contains Cells. So, to read the value from particular cell you should read that value from Cell object instead of Column.

private void button6_Click(object sender, EventArgs e)
{
    string copyPath = @"C:\user\elec\copy";
    if (!System.IO.Directory.Exists(copyPath))
        System.IO.Directory.CreateDirectory(copyPath);
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        if (dataGridView1.Rows[i].Cells[0].Value != null && 
            !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value.ToString()))
        {
            string filePath = dataGridView1.Rows[i].Cells[0].Value.ToString();
            if (System.IO.File.Exists(filePath))
            {                   
                string fileName = System.IO.Path.GetFileName(filePath);
                string newpath = System.IO.Path.Combine(copyPath, fileName);
                System.IO.File.Copy(filePath, newpath, true);
                dataGridView1.Rows[i].Cells[1].Value = newpath;

                try
                { 
                    Using (SqlConnection con = new SqlConnection(@"Data Source=STACY121\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True"))
                    {
                        con.Open();
                        SqlCommand cmd = con.CreateCommand();
                        cmd.CommandText = "update cncinfo set Column1=@Column1,Column2=@Column2 Where id=@id";
                        cmd.Parameters.Add("@Column1",SqlDbType.VarChar).Value =filePath;
                        cmd.Parameters.Add("@Column2",SqlDbType.VarChar).Value =newpath;
                        //you must have the id value in datagridview to update the specific record.
                        cmd.Parameters.Add("@id",SqlDbType.Int).Value =Convert.ToInt32(dataGridView1.Rows[i].Cells["id"].Value);


                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                 }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                } 
            }
            dataGridView1.Rows[i].Cells[0].Value = string.Empty;
        }
    }
}

已编辑:

如果要使用其他文件名进行复制,则可以指定类似的文件名。

if you would like to copy with different file name then you can specify the file name like this.

int iFileNo = 1; // this is the part of file number in sample-rev-01
string fileName = System.IO.Path.GetFileName(filePath);
string ext = new System.IO.FileInfo(filePath).Extension;
//if the file name is sample.txt then it would be return as sample-rev-01.txt
fileName = String.Format("{0}-rev-{1:00}{2}", fileName.Replace(ext, ""), iFileNo, ext);

string newpath = System.IO.Path.Combine(copyPath, fileName);
System.IO.File.Copy(filePath, newpath, true);

这篇关于如何将内容从一个单元复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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