我正在使用Microsoft visual studio 2010在C#中开发一个Windows窗体应用程序。我有一个下载和上传按钮,用于将文件附加到我的本地数据库(网格视图)。我的第二列细胞没问题。但是当上传附件到第三和第四列单元格时,我无法附加和下载文件。你怎么看?我是否也需要这些列的单独按钮。或者,您能否告诉我如何使用表单中的相同按钮上传和下载文件。
我可以将文件附加到第3和第4列,请参阅快照的链接。 http://i60.tinypic.com/xbe0bo.jpg
代码低于
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.IO;
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cncDataSet1.CncInfo' table. You can move, or remove it, as needed.
this.cncInfoTableAdapter.Fill(this.cncDataSet1.CncInfo);
timer1.Start();
}
private void btnUpload_Click(object sender, EventArgs e)
{
try
{
//Throw error if attachment cell is not selected.
//make sure user select only single cell
if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1)
{
UploadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[1].ColumnIndex == 2)
{
UploadAttachment(cncInfoDataGridView.SelectedCells[1]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[2].ColumnIndex == 3)
{
UploadAttachment(cncInfoDataGridView.SelectedCells[2]);
}
else
MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnDownload_Click(object sender, EventArgs e)
{
//Throw error if attachment cell is not selected.
//make sure user select only single cell
//and the cell have a value in it
if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1 && cncInfoDataGridView.SelectedCells[0].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[1].ColumnIndex == 2 && cncInfoDataGridView.SelectedCells[1].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[1]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[2].ColumnIndex == 3 && cncInfoDataGridView.SelectedCells[2].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[2]);
}
else
MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void cncInfoDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//Throw error if attachment cell is not selected.
//make sure user select only single cell
//and the cell have a value in it
if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1 && cncInfoDataGridView.SelectedCells[0].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2 && cncInfoDataGridView.SelectedCells[1].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[1]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[2].ColumnIndex == 3 && cncInfoDataGridView.SelectedCells[2].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[2]);
}
else
MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}}
答案 0 :(得分:1)
我在这里看到的一个问题是这些条件:
cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[1].ColumnIndex == 2
如果您希望只选择一个单元格(SelectedCells.Count == 1
),那么就不会有SelectedCells[1]
或SelectedCells[2]
等......总是{ {1}}。
因此,只需在要引用所选单元格的每个位置使用SelectedCells[0]
(因为您只需要选择一个单元格)。
编辑:顺便说一下,我建议您通过编写如下函数来清理您的代码:
SelectedCells[0]
然后在所有条件下使用:
private bool SelectedNonNullCellFromColumn(int columnId)
{
return cncInfoDataGridView.SelectedCells.Count == 1 &&
cncInfoDataGridView.SelectedCells[0].ColumnIndex == columnId
&& cncInfoDataGridView.SelectedCells[0].Value != null
}
(上传的条件有些不同,因为它们不会检查空值,因此你可以为那些创建类似的功能)
edit2 实际上,您不需要这样做。解决原始问题后,所有if(SelectedNonNullCellFromColumn(1))
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
,Download()
内部条件将始终作用于Upload()
因此您可以将所有条件合并为:
SelectedCells[0]