![](/img/trans.png)
[英]How to load selected rows from sql database to DataGridView in C#?
[英]How to create the zip file from the selected rows of datagridview in c#
我正在将.xml文件中的某些元素显示到DataGridView
,并且工作正常。
这是选择xml文件的代码
//Browse Folder
private void Btn_SelectFolder_Click(object sender, EventArgs e)
{
try
{
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK)
{
DataTable dt = new DataTable();
dt.Rows.Clear();
dt.Columns.Add("Select", typeof(bool));
dt.Columns.Add("File_Name");
dt.Columns.Add("Vendor_Name");
dt.Columns.Add("Vendor_ID");
dt.Columns.Add("Date_Range", typeof(DateTime));
lbl_Path.Text = fbd.SelectedPath;
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.xml");
XmlDocument doc = new XmlDocument();
XmlNodeList nodes = doc.GetElementsByTagName("cfdi:Emisor");
XmlNodeList nodes1 = doc.GetElementsByTagName("cfdi:Comprobante");
foreach (string tot_file in files)
{
doc.Load(tot_file);
string FileName = Path.GetFileNameWithoutExtension(tot_file);
for (int i = 0; i < nodes.Count; i++)
{
string Name = nodes[i].Attributes["Nombre"].Value;
string ID = nodes[i].Attributes["Rfc"].Value;
string Date = nodes1[i].Attributes["Fecha"].Value;
DataRow row = dt.NewRow();
row["File_Name"] = FileName;
row["Vendor_Name"] = Name;
row["Vendor_ID"] = ID;
row["Date_Range"] = Date;
dt.Rows.Add(row);
}
}
XML_Grid.DataSource = dt;
txt_FileName.ReadOnly = false;
txt_Name.ReadOnly = false;
txt_ID.ReadOnly = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
现在,我想要从DataGridView
选择行并创建所选文件的zip文件。
我已经尝试过以下代码来创建zip文件:
//Create Zip
private void Btn_SaveZip_Click(object sender, EventArgs e)
{
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string folderToZip = lbl_Path.Text;
string zipFile = saveFileDialog1.FileName + ".zip";
using (ZipArchive zipArchive = ZipFile.Open(zipFile, ZipArchiveMode.Create))
{
DirectoryInfo di = new DirectoryInfo(folderToZip);
FileInfo[] filesToArchive = di.GetFiles();
if (filesToArchive != null && filesToArchive.Length > 0)
{
foreach (FileInfo fileToArchive in filesToArchive)
{
zipArchive.CreateEntryFromFile(fileToArchive.FullName, fileToArchive.Name, CompressionLevel.Optimal);
}
}
}
MessageBox.Show("Zip File Successfully Created", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
}
上面的代码可以正常工作并创建zip文件,但是它是直接从路径中检查文件并创建该文件夹的zip文件。
如何从DataGridview
而不是直接从文件夹中选择文件,以及如何创建所选文件的zip文件。
任何帮助都会对我有所帮助,我已经尝试使它工作数小时了。
如果我正确理解了您的问题,则只想将在数据网格中选中“ Select
列的那些文件保存到一个zip文件中。
如果是这样,对您的Zip保存例程进行以下修改可以帮助您:
using (ZipArchive zipArchive = ZipFile.Open(zipFile, ZipArchiveMode.Create))
{
List<FileInfo> filesToArchive = new List<FileInfo>();
DirectoryInfo di = new DirectoryInfo(folderToZip);
// loop through all of the rows in the data grid
foreach (DataGridViewRow row in XML_Grid.Rows)
{
DataRowView rowView = row.DataBoundItem as DataRowView;
if (rowView == null) continue; // new row or not bound, ignore
var selectedValue = rowView["Select"]; // get the value for the select column
if (selectedValue == DBNull.Value || selectedValue == null || !(bool)selectedValue) // ignore null or false
continue;
string fileName = rowView["File_Name"].ToString(); // get the file name
var files = di.GetFiles(fileName + ".*"); // we had no extension so search the folder to get the full path
filesToArchive.AddRange(files); // add those file(s) to the list to archive
}
if (filesToArchive != null && filesToArchive.Count > 0)
{
foreach (FileInfo fileToArchive in filesToArchive)
{
zipArchive.CreateEntryFromFile(fileToArchive.FullName, fileToArchive.Name, CompressionLevel.Optimal);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.