【发布时间】:2014-08-24 02:50:28
【问题描述】:
我正在尝试检查是否有任何 column1,任何单元格不为空。我想让然后清空并将文件复制到下一个列单元格。什么,我的想法是检查一个特定的 Column1-所有单元格是否可以说“COLUMN1”其中一个单元格不为空。然后我需要将文件 [我已将文件路径附加到该特定单元格] 复制到下一个 column2。同时我想将文件复制到桌面上的文件夹假设位置是 C:/user/elec/copy,我想擦除 Column1 -cell 数据。
我该怎么做。链接我正在尝试做的事情..
编辑编码.....
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;
}
}
}
【问题讨论】:
-
那么,您的实际问题是什么?
-
@JohnSaunders 我不知道该怎么做..请帮帮我..我想将一个单元格内容复制到另一列单元格内容..如何做到这一点..
-
做一些研究。不要只是谷歌的例子。去学习 DataGridView 类。问题:
DataGridView的单元格的数据类型是什么?它是否有任何与剪贴板有关的属性或方法? -
@Shell howzz going ...你能帮我解决这个问题吗..拜托了
-
@Shell 我也想将新位置和数据更新到数据库中。请帮帮我.. 这样我就可以关闭整个主题.. 非常感谢 Shell
标签: c# winforms datagridview