我试图仅导出数据视图上选定的复选框项。我现在的代码可以工作,但问题是它导出了所有的东西,我可以在导出的csv文件中看到真值/假值,但是在我的生活中,我不知道如何导出真正的值,而不是所有的东西。下面列出了示例代码。
private void GetCellData()
{
string data = "";
string userDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
TextWriter tw = new StreamWriter(userDesktop + "\\" + "export.csv");
// Count each row in the datagrid
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["Selection_Box"].Value != null &&
(bool)dataGridView1.Rows[i].Cells["Selection_Box"].Value)
{
foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells)
{
data += (cell.Value + ",");
}
data += "\n";
}
else
{
continue;
}
}
tw.WriteLine(data, "data");
tw.Close();
}数据"Selection_Box“上的复选框是一个DataGridViewCheckBoxColumn。ExampleExport只是链接到一个名为“导出”的按钮。当用户选择数据集中的复选框并单击“导出”时,会将.csv文件转储到桌面上,其值类似于下面列出的值。
True,3,1,管道,手动,RTD,2,45 Ax
True,4,1,管道,手动,RTD,2,60 Ax
真,5,1,管道,手册,RTD,1.5,45 C,
假,6,1,管道,手动,RTD,2,45 Ax,
假,8,1,管道,手动,RTD,1.5,45 C,
假,29,1,管道,手册,RTD,2,45 C
编辑:谢谢你为我指出正确的方向,非常感谢。最后,我将if语句调整为:
if (dataGridView1.Rows[i].Cells["Selection_Box"].Value != null &&
(bool)dataGridView1.Rows[i].Cells["Selection_Box"].Value)现在它正在转储选定的值。
发布于 2011-06-16 11:23:34
应该检查CheckBox列中的值,如下所示
if((bool) row.Cells["Column7"] as DataGridViewCheckBoxCell).FormattedValue)如果为true,则需要追加行的值。
发布于 2011-06-16 11:22:33
像这样的东西在第一个街区里.
if(((bool)dataGridView1.Rows[i].Cells[0]) == true)
{
// Loop through and get the values
foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells)
{
data = data + (cell.Value + ",");
}
data += "\n";
}
else
{
// else block not really necessary in this case, but illustrates the point....
continue;
}发布于 2016-02-23 17:44:07
你可以用这种方式来确认它是否是真的。
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Selection_Box"].Value) == true)https://stackoverflow.com/questions/6370860
复制相似问题