[英]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.