dataGridView1第0列是checkbox 选中则下载。
private void button5_Click(object sender, EventArgs e)
{
int j = dataGridView1.Rows.Count;
Boolean find = false;
for (int i = 0; i < j; ++i)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
{
find = true;
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = dataGridView1.Rows[i].Cells[4].Value.ToString(); //获取文件名
string str = dataGridView1.Rows[i].Cells[3].Value.ToString(); //获取文件后缀名
switch (str)
{
case ".doc":
sfd.Filter = "word文档(*.doc)|*.Doc";
break;
case ".xls":
sfd.Filter = "xls文档(*.xls)|*.xls";
break;
case ".txt":
sfd.Filter = "txt文档(*.txt)|*.txt";
break;
case ".jpg":
sfd.Filter = "图片文档(*.jpg)|*.jpg";
break;
case ".bmp":
sfd.Filter = "图片文档(*.bmp)|*.bmp";
break;
case ".gif":
sfd.Filter = "图片文档(*.gif)|*.gif";
break;
default:
sfd.Filter = "其他格式|*." + dataGridView1.Rows[i].Cells[10].Value.ToString();
break;
}
try
{
sqlDataAdapter1.SelectCommand.CommandText = "select ProjectContent from content where id='" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "'";
DataSet dataset = new DataSet();
sqlDataAdapter1.Fill(dataset, "content");
byte[] mydata = null;
mydata = (byte[])dataset.Tables[0].Rows[0]["ProjectContent"];
Int32 size = mydata.GetUpperBound(0);
string filepath;
if (sfd.ShowDialog() == DialogResult.OK)
{
sfd.FilterIndex = 1;
filepath = sfd.FileName;
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(mydata, 0, size + 1);
fs.Close();
MessageBox.Show("保存文件成功!", "提示");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "错误提示");
}
}
}
if (find == false)
{
MessageBox.Show("请选中要下载的文档!", "提示");
}
}