[英]How can I get the name of the current selected datagridview when I’ve multiple datagridviews in C#?
I have 5 datagridviews and I want to get details from the selected datagridview.我有 5 个数据网格视图,我想从选定的数据网格视图中获取详细信息。
Like when selecting the gridview row, I want to take that row details with the gridview name.就像选择 gridview 行时一样,我想使用 gridview 名称获取该行的详细信息。
private void assigncontrctbtn_Click(object sender, EventArgs e)
{
SqlConnection conn;
conn = ConnectionManager.GetConnection();
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
conn.Open();
string hh = "SELECT contractstatus from dbo.contracts where "here i need gridview name";
SqlCommand cmd = new SqlCommand(hh, conn);
string dd = Convert.ToString(cmd.ExecuteScalar());
if ((dd == "Contract logged") && (cmbCategory.Text != "Calculate Contract") && (cmbCategory.Text == "Verify Contract"))
{
MessageBox.Show("no");
}
else if ((dd == "Calculate Contract") && (cmbCategory.Text != "Verify Contract") && (cmbCategory.Text == "Calculate Contract"))
{
MessageBox.Show("verify");
}
else
{
if (dba.logcontract(cntrctnmecmb.Text, Convert.ToInt32(prcecodecmb.Text), seasoncmb.Text, cmplexitycmb.Text, revisecmb.Text, logdatetime, condatefrm.Value.Date.ToString("MM/dd/yyyy"), condteto.Value.Date.ToString("MM/dd/yyyy"), vatcmb.Text, statuscmb.Text, lstcmnt.Text, apprvedbycmb.Text, cmbCategory.Text))
{
MessageBox.Show("Successfully Added");
FillGridCal();
FillGridVer();
}
else
{
MessageBox.Show("Error occured");
}
}
}
You can have an an event say CellClick
for each of your DataGridView.您可以为每个 DataGridView 设置一个事件,例如
CellClick
。 When a row is clicked, you can get the details that you want.单击一行时,您可以获得所需的详细信息。 I'm assuming that what you mean by
take that row details
is take the cell values for the selected row
.我假设您所说的
take that row details
意思是take the cell values for the selected row
。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
string firstColumn = (string)dataGridView1.Rows[e.RowIndex].Cells[firstColumn.Name].Value;//I'm getting 1 column from the selected row, you can do a loop if you want to get all cells
string dataGridView = dgv.Name;//This is the DataGridView name
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.