将 DataGridView 中的选定行/单元格值从一个窗体检索到另一个窗体

本文关键字:窗体 一个 检索 另一个 DataGridView 单元格 | 更新日期: 2023-09-27 18:34:08

我是C#.net的新手。我想将 dataGridView 单元格单击单元格值获取到另一种形式的文本框,但找不到正确的方法。我有两种形式 1.UserPanelfrm 和 2.frmAddProjects .UserPanelFrm 由 dataGridView 组成,它显示来自数据库的项目列表。我想显示双击单元格的单元格信息,以显示在 frmAddProjects 的文本框中。

项目类的代码.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();

单元格单击代码

    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 code:

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

将 DataGridView 中的选定行/单元格值从一个窗体检索到另一个窗体

如果要

在双击后在另一种形式的文本框中显示 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 中的所有这些字段都是公共的。通过您的代码,它们似乎是"静态的",这将是在实例化此类对象之前设置它们的唯一方法,但我这样做就好像它们只是公共属性一样。