我试着检查是否有任何column1,任何一个单元格都不为空。我想制作然后为空并将文件复制到下一个列单元格。 什么,我的想法是检查一个特定的Column1- 所有单元格是否允许说“COLUMN1”其中一个单元格不为空。 然后我需要文件[我已将文件路径附加到该特定单元格]以复制到下一个column2。同时我想将文件复制到桌面上的文件夹中 假设位置是 C:/ user / elec / copy ,我想删除Column1 -cell数据。
我该怎么办?链接我正在尝试做什么.. https://imageshack.com/i/p58FpXoUj
我正在尝试的代码
private void button6_Click(object sender, EventArgs e)
{
if(dataGridView1.Columns["column1"].Cell[i] != string.Empty)
{
dataGridView1.row.Cells[0] = dataGridView1.Columns["column2"].Cells[0];// trying to copy the path from column1 cell to column2 cell
//error above line at row and cells
string fileName = Convert.ToString(e.Value);//<<-- error here in value
if (!string.IsNullOrEmpty(fileName))
{
byte[] objData;
FileInfo fileInfo = new FileInfo(fileName);
string fileExtension = fileInfo.Extension;
string s = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();// error here in rowindex..
objData = File.ReadAllBytes(s);
File.WriteAllBytes("C:/user/elec/copy", s); //trying to download the file from column1---<< error here
}
dataGridView1.row.Cells[0] = string.Empty;
}
}
答案 0 :(得分:1)
我解决了这个问题!!!!
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 (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
dataGridView1.Rows[i].Cells[2].Value = string.Empty;
}
}
}