首页 > 解决方案 > 从datagridview中选定行的值的ID中获取名称

问题描述

单击网格视图时

在此处输入图像描述

我在 gridview 中隐藏 Author_ID 和 Publisher_ID 。我希望当我单击一行时,作者和发布者组合框文本填充选定的行值。行中有发布者和作者 ID 作为外键,但我想在组合框文本上显示他们的名字。如何访问它们。

    private  void gridViewBook_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

        gridViewBook.CurrentRow.Selected = true;

        txtID.Text = gridViewBook.Rows[e.RowIndex].Cells["BOOK_ID"].FormattedValue.ToString();
        txtISBN.Text = gridViewBook.Rows[e.RowIndex].Cells["ISBN"].FormattedValue.ToString();
        txtName.Text = gridViewBook.Rows[e.RowIndex].Cells["BOOK_NAME"].FormattedValue.ToString();
        txtPage.Text = gridViewBook.Rows[e.RowIndex].Cells["PAGE"].FormattedValue.ToString();
        txtLang.Text = gridViewBook.Rows[e.RowIndex].Cells["LANGUAGE"].FormattedValue.ToString();
        comboBoxAuthor.Text= gridViewBook.Rows[e.RowIndex].Cells["AUTHOR_ID"].FormattedValue.ToString();
        
        comboBoxPublisher.Text = gridViewBook.Rows[e.RowIndex].Cells["PUBLISHER_ID"].FormattedValue.ToString();
    }

我只能访问他们的 ID。我像这样填充了组合框。

    private void fillAuthorBox()
    {
        conn.Open();

        SqlCommand cmd = new SqlCommand("SELECT * FROM AUTHOR ", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        comboBoxAuthor.ValueMember = "AUTHOR_ID";
        comboBoxAuthor.DisplayMember = ("AUTHOR_FULLNAME");
        comboBoxAuthor.DataSource = dt;

        conn.Close();
    }

    private void fillPublisherBox()
    {
        conn.Open();

        string query = " SELECT * FROM PUBLISHER ";
        SqlCommand cmd = new SqlCommand(query, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        comboBoxPublisher.ValueMember = "PUBLISHER_ID";
        comboBoxPublisher.DisplayMember = "PUBLISHER_NAME";
        comboBoxPublisher.DataSource = dt;

        conn.Close();

    }

标签: c#.netsql-serverwinforms

解决方案


我在看fillAuthorBox()fillPublisherBox()第一。在某处你有调用这些方法的代码。我们在问题中看不到这一点,所以我假设它是一个通用的表单加载方法,如下所示:

void form_Load(...)
{
    fillAuthorBox();
    fillPublisherBox();
}

我们可以做得更好。这些方法有一些常见的错误,我将在下面更正:

//only re-use the connection string, NOT the whole connection object
private connString As String = "connection string here";

//return a value, don't touch any form controls
// even better if these methods are moved to a different class
public DataTable getAuthorNames()
{        
    using (var conn = new SqlConnection(connString))
    // Don't SELECT * if you don't need all the fields!
    using (var cmd = new SqlCommand("SELECT AUTHOR_ID, AUTHOR_FULLNAME FROM AUTHOR ", conn))
    using (var da = new SqlDataAdpater(cmd))
    { 
        var dt = new DataTable();
        da.Fill(dt); // Fill() automatically opens and closes the connection object
        return dt;
    } 
    //using blocks make sure your items are fully disposed when the block closes, 
    // **even if an exception is thrown!**
}

public DataTable getPublisherNames()
{
    ​using (var conn = new SqlConnection(connString))
    using (var cmd = new SqlCommand("SELECT PUBLISHER_ID, PUBLISHER_NAME FROM PUBLISHER", conn))
    using (var da = new SqlDataAdpater(cmd))
   ​ { 
        ​var dt = new DataTable();
       ​ da.Fill(dt);
       ​ return dt;
   ​ }     
}

现在我原来的 form_Load() 看起来像这样:

void form_Load(...)
{
    comboBoxAuthor.ValueMember = "AUTHOR_ID";
    comboBoxAuthor.DisplayMember = "AUTHOR_FULLNAME";
    comboBoxAuthor.DataSource = getAuthorNames();

    comboBoxPublisher.ValueMember = "PUBLISHER_ID";
    comboBoxPublisher.DisplayMember = "PUBLISHER_NAME";
    comboBoxPublisher.DataSource = getPublisherNames();
}

当有人单击网格中的一行时,我们可以从这里继续选择正确的条目。为此,Text我们不想设置属性,而是更改选择了哪个项目。由于我们知道ValueItem,我们可以通过SelectedValue属性来做到这一点。

private  void gridViewBook_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    gridViewBook.CurrentRow.Selected = true;

    txtID.Text = gridViewBook.Rows[e.RowIndex].Cells["BOOK_ID"].FormattedValue.ToString();
    txtISBN.Text = gridViewBook.Rows[e.RowIndex].Cells["ISBN"].FormattedValue.ToString();
    txtName.Text = gridViewBook.Rows[e.RowIndex].Cells["BOOK_NAME"].FormattedValue.ToString();
    txtPage.Text = gridViewBook.Rows[e.RowIndex].Cells["PAGE"].FormattedValue.ToString();
    txtLang.Text = gridViewBook.Rows[e.RowIndex].Cells["LANGUAGE"].FormattedValue.ToString();

    comboBoxAuthor.SelectedValue = gridViewBook.Rows[e.RowIndex].Cells["AUTHOR_ID"].Value;        
    comboBoxPublisher.SelectedValue = gridViewBook.Rows[e.RowIndex].Cells["PUBLISHER_ID"].Value;
}

但是这里要小心!我对 WinForms 有点不习惯,我不记得这对于不匹配的类型会有多大的宽容度。组合框很可能ValueMemberintor DataGridViewCell,但我们使用 astring来设置SelectedValue, 可能效果不佳。您可能必须将字符串转换回 int,或者找到值的索引来更新 SelectedIndex 属性。


推荐阅读