日期:2014-05-17 浏览次数:21363 次
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(string));
DataRow dr = null;
dr = dt.NewRow();
dr["name"] = "guwei4037";
dr["age"] = 20;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "guwei4037";
dr["age"] = 20;
dt.Rows.Add(dr);
this.dataGridView1.DataSource = dt;
this.dataGridView1.AllowUserToAddRows = false;//设置这个为了将默认产生的行去除,否则结果多1行
string value = "guwei4037";//假设这是你得到的点击的cell的值
MessageBox.Show(this.dataGridView1.Rows.OfType<DataGridViewRow>().Select(x => (string)x.Cells["name"].Value == value).ToList().Count + "");//这里输出列name当中guwei4037的数量 添加了2次,所以为2
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
textBox1.Text=dt.AsEnumerable().Where(a => a.Field<string>("姓名").Equals(dataGridView1.SelectedRows[0].Cells["姓名"].Value.ToString();)).Count();