简体   繁体   English

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

[英]How to get the data when I select a datagridview cell or column

I have 2 datagridview controls in a Windows Forms application. 我在Windows Forms应用程序中有2个datagridview控件。
I would like to get the info of a person when I select first datagridview cell or row to next 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];

}

Im trying the above code. 我正在尝试上面的代码。 But it's not working. 但这不起作用。

I'm not too sure what DailyGV.CurrentRow is but basically you can use the RowHeaderMouseClick ...see MSDN documentation . 我不太确定DailyGV.CurrentRow是什么,但是基本上您可以使用RowHeaderMouseClick ...请参阅MSDN文档 To use it, hook an event handler to it when initializing the form components (you can use VS designer as well... 要使用它,请在初始化表单组件时将事件处理程序挂钩到它(您也可以使用VS设计器...

dataGridView1.RowHeaderMouseClick += dataGridView1_RowHeaderMouseClick;

void dataGridView1_RowHeaderMouseClick(
object sender, DataGridViewCellMouseEventArgs e)
{
}

this event handler will get fired everytime you select a row header in the DataGridView control which will pass information about the event through an instance of the DataGridViewCellMouseEventArgs class (see MSDN documentation ). 每当您在DataGridView控件中选择一个行标题时,都会触发该事件处理程序,该DataGridView将通过DataGridViewCellMouseEventArgs类的实例传递有关事件的信息(请参阅MSDN文档 )。 This argument has a RowIndex property that provides the index of the row clicked which you can use to retrieve the values of the cells in that row...including the person id (if provided)...for example... 此参数具有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
}

notice that you need to provide a proper column name when access the cells collection indexer... Cells["columnName"] 请注意,在访问单元格集合索引器时,您需要提供适当的列名。Cells Cells["columnName"]

I will not give any description about the solution because Leo and Arsalan Bhatti has already suggested the solution. 由于Leo和Arsalan Bhatti已经提出了解决方案,因此我将不提供任何解决方案的描述。 I am just telling you how your code should looks like and how it should be written. 我只是在告诉您您的代码应该是什么样子以及应该如何编写。

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
    {}
}
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);
         }

You are passing the current row's index as employee id in SELECT query. 您正在SELECT查询中将当前行的索引作为员工ID传递。 Pass employee id for the selected record. 传递所选记录的员工ID。 Then it will work fine. 然后它将正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过列名获取DataGridView的单元格值? - How to get cell value of DataGridView by column name? 当 DataGridView 绑定到 DataTable 时,如何在编辑 DataGridView 单元格后更新 object 数据? - How do I update object data after editing a DataGridView cell when the DataGridView is bound to a DataTable? 如何使数据显示在正确的DataGridView列中? - How do I get my data to show in correct DataGridView column? 当用户单击该行的单元格时,如何选择完整的 dataGridView 行? - How do I select a complete dataGridView Row when the user clicks a cell of that row? 如何使用c#中的行索引和列索引从datagridview获取单元格值? - How do I get the cell value from a datagridview using row index and column index in c#? DataGridView:MultiSelect为true时如何选择当前行中的第一个单元格 - DataGridView: How to select first cell in current row when MultiSelect is true 如何在datagridview中选择特定的彩色单元格? - How do I select a specific colored cell in datagridview? 当我单击datagridview中的行时如何从datagridview获取任何数据? - How to get any data from datagridview when I click on the row in datagridview? 当我按 Enter 时,dataGridView 获取更新单元格的值 - dataGridView get the value of an updated cell when I press enter 如何获取填充了sql数据的datagridview的选定单元格值? - How to get the selected cell value of a datagridview filled with sql data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM