C#DataGridView.Rows.ToString()
我试图将每一行的值存储在字符串中 如果我尝试执行C#DataGridView.Rows.ToString(),c#,string,datagridview,tostring,C#,String,Datagridview,Tostring,我试图将每一行的值存储在字符串中 如果我尝试执行DataGridView.Rows.ToString() System.Windows.Forms.DataGridViewRowCollection 甚至不接近我需要的 有什么想法吗?你需要这样做: StringBuilder sb = new StringBuilder(); for(int row=0; row<DataGridView.Rows.Count; row++) { for(int col=0; col <
DataGridView.Rows.ToString()
System.Windows.Forms.DataGridViewRowCollection
甚至不接近我需要的
有什么想法吗?你需要这样做:
StringBuilder sb = new StringBuilder();
for(int row=0; row<DataGridView.Rows.Count; row++)
{
for(int col=0; col < DataGridView.Columns.Count; col++)
{
sb.Append(DataGridView.Row[row][col].ToString());
}
}
sb.ToString(); // that will give you the string you desire
StringBuilder sb=新建StringBuilder();
for(int row=0;row使用for each语句迭代Datagridview中的每一行
foreach (DataGridViewRow datarow in dataGridView.Rows)
{
string col1 = datarow.Cells["Col1"].Value.ToString();
string col2 = datarow.Cells["Col2"].Value.ToString();
string col3 = datarow.Cells["Col3"].Value.ToString();
}
类似于上面的代码。请原谅在iPad上键入的格式设置。我认为您是在按行查找内容。如果是这样,我建议如下
private static IEnumerable<string> GetRowValues(this DataGridView dgv)
{
var list = new List<string>(dgv.Rows.Count);
foreach (DataGridViewRow row in dgv.Rows)
{
var sb = new StringBuilder();
foreach (DataGridViewCell cell in row.Cells)
{
sb.Append(cell.ToString());
}
list.Add(sb.ToString());
}
return list.AsReadOnly();
}
私有静态IEnumerable GetRowValues(此DataGridView dgv)
{
var list=新列表(dgv.Rows.Count);
foreach(dgv.Rows中的DataGridViewRow行)
{
var sb=新的StringBuilder();
foreach(row.Cells中的DataGridViewCell单元格)
{
sb.Append(cell.ToString());
}
添加(sb.ToString());
}
return list.AsReadOnly();
}
您使用的是默认网格吗?您可能需要在行集合
中的每个单元格中循环执行可能的重复项@Ezi:我不知道您想问什么。我的表单上有一个DataGridView,其中满是列和数据。+1用于使用StringBuilder并为OP提供所有行或单行选项。
private static IEnumerable<string> GetRowValues(this DataGridView dgv)
{
var list = new List<string>(dgv.Rows.Count);
foreach (DataGridViewRow row in dgv.Rows)
{
var sb = new StringBuilder();
foreach (DataGridViewCell cell in row.Cells)
{
sb.Append(cell.ToString());
}
list.Add(sb.ToString());
}
return list.AsReadOnly();
}