C#中有多个datagridview时,如何获取当前选中的datagridview的名称?
How can I get the name of the current selected datagridview when I’ve multiple datagridviews in C#?
我有 5 个数据网格视图,我想从选定的数据网格视图中获取详细信息。
就像选择 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");
}
}
}
您可以为每个 DataGridView 创建一个事件 CellClick
。单击一行时,您可以获得所需的详细信息。我假设你所说的 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
}
我有 5 个数据网格视图,我想从选定的数据网格视图中获取详细信息。
就像选择 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");
}
}
}
您可以为每个 DataGridView 创建一个事件 CellClick
。单击一行时,您可以获得所需的详细信息。我假设你所说的 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
}