判断datagridview某列是否含有某字段
现在想循环遍历一个datagridview,判断其某列的值中是否含有“辊道”两字,如果有且其数量《15,则开关箱尺寸为600*400*1200,数量>15开关型尺寸为800*400*1200 datagridview遍历 单元格含有某字段
[解决办法]
DataTable dt = new DataTable();
dt.Columns.Add("M_name", typeof(string));
dt.Columns.Add("M_number", typeof(string));
dt.Columns.Add("开关箱尺寸", typeof(string));
dt.Rows.Add("滚到", "12", "");
dt.Rows.Add("送辊", "1", "");
dt.Rows.Add("滚辊道到", "162", "");
this.dataGridView1.DataSource = dt;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
DataGridViewRow g = this.dataGridView1.Rows[i];
if (g.Cells["M_name"].Value != null && g.Cells["M_name"].Value.ToString().Contains("辊道"))
{
if (g.Cells["M_number"].Value != null)
{
if (Convert.ToInt32(g.Cells["M_number"].Value) > 15)
{
g.Cells["开关箱尺寸"].Value = "800*400*1200";
}
else
{
g.Cells["开关箱尺寸"].Value = "600*400*1200";
}
}
}
}
[解决办法]
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
string str = dataGridView1.Rows[i].Cells["M_name"].Value.ToString();
if (str.Contains("辊道"))
{
if (Convert.ToInt32(dataGridView1.Rows[i].Cells["M_number"].Value.ToString()) > 15)
{
dataGridView1.Rows[i].Cells["开关箱尺寸"].Value = "800*400*1200";
}
else
{
dataGridView1.Rows[i].Cells["开关箱尺寸"].Value = "600*400*1200";
}
}
}
[解决办法]
dataGridView1.Rows[i].Cells["M_name"].Value.ToString 在用 contain是否包含