我是C#.net的新手。我想让dataGridView单元格单击单元格值到另一个表单的文本框,但无法找到正确的方法。我有两个表单1. UserPanelfrm和2. frmAddProjects。 UserPanelFrm由dataGridView组成,它显示了数据库中的项目列表。我想显示双击单元格的单元格信息,以便在frmAddProjects的文本框中显示。
ProjectClass.cs的代码
public class ProjectClass
{
SqlConnection conn = new SqlConnection(DataConnectionClass.DbConnection);
public int ManageProject(int ProjectID, String BusinessName, String ContactPerson, String Phone, String Address, String CreatedDate, String AssignedTo, String Mode)
{
try
{
int result = 0;
SqlCommand cmd = new SqlCommand("sp_ManageProject", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProjectID", ProjectID);
cmd.Parameters.AddWithValue("@BusinessName", BusinessName);
cmd.Parameters.AddWithValue("@ContactPerson", ContactPerson);
cmd.Parameters.AddWithValue("@Phone", Phone);
cmd.Parameters.AddWithValue("@Address", Address);
cmd.Parameters.AddWithValue("@CreatedDate", CreatedDate);
cmd.Parameters.AddWithValue("@AssignedTo", AssignedTo);
cmd.Parameters.AddWithValue("@Mode", Mode);
conn.Open();
result = cmd.ExecuteNonQuery();
conn.Close();
return result;
}
catch (Exception ex)
{
throw ex;
}
}
public DataTable SelectAllProjects()
{
try
{
SqlCommand cmd = new SqlCommand("Select ID,ProjectID,BusinessName,ContactPerson,Phone,Address,CreatedDate,AssignedTo from tbl_ProjectInformation", conn);
DataTable dt = new DataTable();
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
conn.Close();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
}
将ProjectList调用到UserPanelfrm的代码dgvProjectDetails:
ProjectClass pc = new ProjectClass();
dgvProjectDetails.DataSource = pc.SelectAllProjects();
cellclick的代码
private void dgvProjectDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = null;
foreach (DataGridViewCell selectedCell in dgvProjectDetails.SelectedCells)
{
cell = selectedCell;
break;
}
if (cell != null)
{
DataGridViewRow row = cell.OwningRow;
frmAddProjects.projectid = row.Cells["ProjectID"].Value.ToString();
frmAddProjects.businessname = row.Cells["BusinessName"].Value.ToString();
frmAddProjects.contactperson = row.Cells["ContactPerson"].Value.ToString();
frmAddProjects.phone = row.Cells["Phone"].Value.ToString();
frmAddProjects.address = row.Cells["Address"].Value.ToString();
frmAddProjects.createddate = row.Cells["CreatedDate"].Value.ToString();
frmAddProjects.assignedto= row.Cells["AssignedTo"].Value.ToString();
// etc.
frmAddProjects ProjectDetail = new frmAddProjects();
ProjectDetail.ShowDialog();
}
}
frmAddProjects代码:
public static string projectid, businessname,contactperson,phone,address,createddate,assignedto;
public frmAddProjects()
{
InitializeComponent();
}
public void fillRecord()
{
txtProjectID.Text = projectid;
txtBusinessName.Text = businessname;
txtContactPerson.Text = contactperson;
txtPhone.Text = phone;
txtAddress.Text = address;
dTPCreatedDate.Text = createddate;
cmbAssignedTo.Text = assignedto;
}
答案 0 :(得分:2)
如果要在另一种形式的TextBox中显示双击后GridDataView中单元格的内容,首先必须使用CellMouseDoubleClick事件。
建议:
private void dgvProjectDetails_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
int col = e.ColumnIndex;
int row = e.RowIndex;
DataGridView dgv = sender as DataGridView;
if (dgv == null)
return;
var cell = dgv[col, row];
if (cell != null)
{
DataGridViewRow row = cell.OwningRow;
frmAddProjects ProjectDetail = new frmAddProjects(); //<= THIS must be created first
ProjectDetail.projectid = row.Cells["ProjectID"].Value.ToString();
ProjectDetail.businessname = row.Cells["BusinessName"].Value.ToString();
ProjectDetail.contactperson = row.Cells["ContactPerson"].Value.ToString();
ProjectDetail.phone = row.Cells["Phone"].Value.ToString();
ProjectDetail.address = row.Cells["Address"].Value.ToString();
ProjectDetail.createddate = row.Cells["CreatedDate"].Value.ToString();
ProjectDetail.assignedto= row.Cells["AssignedTo"].Value.ToString();
// etc.
ProjectDetail.ShowDialog();
}
}
我假设frmAddProjects中的所有字段都是公开的。通过你的代码,它们似乎是&#34; static&#34;,这是在实例化这种类型的对象之前设置它们的唯一方法,但我做的就像它们只是公共属性一样。