获取datagrid中光标所在行的某列的值

时间:2021-09-21 16:22:10

该函数响应光标移动事件,并能模糊查询数据表中匹配前两个字母的列

private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
  {
    this.listBox1.Items.Clear();
   DataView dv=new DataView();
   string  aa=dt.Rows[this.dataGrid1.CurrentRowIndex]["customerid"].ToString();//获取datagrid中当前光标所在行的某列
   aa=aa.Substring(0,2);
   DataRow[] drfound=this.dt.Select("customerid like '"+aa+"*'");
   foreach(DataRow dr in drfound)
   {
    this.listBox1.Items.Add(dr["customerid"]);
   }
  
   this.listBox1.Refresh();
   
  }