我选择datagridview单元格或列时如何获取数据

时间:2014-09-12 05:43:44

标签: c# winforms datagridview

我在Windows窗体应用程序中有2个datagridview控件 当我选择第一个datagridview单元格或行到下一个datagridview时,我想得到一个人的信息:

try
{

    ConnectionStringSettings consettings =                                                        ConfigurationManager.ConnectionStrings["attendancemanagement"];                                                                     
    string connectionString = consettings.ConnectionString;
    SqlConnection con = new SqlConnection(connectionString);
    con.Open();
    adap3 = new SqlDataAdapter(@"SELECT   Date,Attendance,Remarks FROM dailyattendance where employee_id='"+DailyGV.CurrentRow+"'", con);

    ds3 = new DataSet();
    adap3.Fill(ds3, "dailyattendance");
    dataGridView1.DataSource = ds3.Tables[0];

}

我正在尝试上面的代码。但它不起作用。

4 个答案:

答案 0 :(得分:0)

我不太确定DailyGV.CurrentRow是什么,但基本上您可以使用RowHeaderMouseClick ...请参阅MSDN documentation。要使用它,在初始化表单组件时将事件处理程序挂钩(您也可以使用VS设计器......

dataGridView1.RowHeaderMouseClick += dataGridView1_RowHeaderMouseClick;

void dataGridView1_RowHeaderMouseClick(
object sender, DataGridViewCellMouseEventArgs e)
{
}

每次在DataGridView控件中选择行标头时,都会触发此事件处理程序,该控件将通过DataGridViewCellMouseEventArgs类的实例传递有关事件的信息(请参阅MSDN documentation)。此参数具有RowIndex属性,该属性提供单击的行的索引,您可以使用该索引来检索该行中单元格的值...包括人员ID(如果提供)...例如..

void dataGridView1_RowHeaderMouseClick(
object sender, DataGridViewCellMouseEventArgs e)
{
      string personId = dataGridView1.Rows[e.RowIndex].Cells["PersonId"].Value;

      //TODO: implement your own query to retrieve data for that person id
}

请注意,在访问单元格集合索引器时需要提供正确的列名... Cells["columnName"]

答案 1 :(得分:0)

我不会对解决方案作任何描述,因为Leo和Arsalan Bhatti已经提出了解决方案。我只是告诉你代码应该是什么样子以及它应该如何编写。

string connectionString = consettings.ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
    try
    {
        con.Open();
        string empID = DailyGV.CurrentRow.Cells["employee_id"].Value.ToString();
        SqlCommand Cmd = con.CreateCommand();
        Cmd.CommandText = "SELECT   Date,Attendance,Remarks FROM dailyattendance where employee_id=@employee_id";
        Cmd.Parameters.Add("@employee_id", SqlDbType.Int).Value = Int32.Parse(empID);
        adap3 = new SqlDataAdapter(Cmd);


        DataTable dt = new DataTable();
        adap3.Fill(dt);
        dataGridView1.DataSource = dt;
        con.Close();
    }    
    catch
    {}
}

答案 2 :(得分:0)

try
         {
             cn.Open();
             string query = "select employee_id,Employee_Name,Image_of_Employee from Employee_Details where employee_id='" + dataGridView1.SelectedCells[0].Value.ToString() + "'";
             SqlCommand cmd = new SqlCommand(query, cn);
             SqlDataReader sdr;
             sdr = cmd.ExecuteReader();
             if (sdr.Read())
             {
                 string aa = (string)sdr["employee_id"];
                 string bb = (string)sdr["employee_name"];
                 txtEmployeeID.Text = aa.ToString();
                 txtnameofemployee.Text = bb.ToString();

                 byte[] img=(byte[])sdr["Image_of_employee"];
                 MemoryStream ms=new MemoryStream(img);
                 ms.Seek(0,SeekOrigin.Begin);
                 pictureBox1.Image=Image.FromStream(ms);   cn.Close();
             }


         }
         catch (Exception e1)
         {
             MessageBox.Show(e1.Message);
         }

答案 3 :(得分:-1)

您在SELECT查询中将当前行的索引作为员工ID传递。传递所选记录的员工ID。然后它会正常工作。