c#winform在文本框中单击“行加载记录”后,在gridview中显示少量列

c#winform在文本框中单击“行加载记录”后,在gridview中显示少量列,c#,winforms,datagridview,C#,Winforms,Datagridview,经过多次搜索,我来这里寻求帮助。在我的C#winform应用程序中我想创建一个dataGridview在用户在数据库中执行搜索后加载一些记录,一旦用户单击行,它应该在文本框中显示所有记录 我有20多个文本框,很少有combobox和datetimepicker,但我只想在dataGridView中显示几列 我知道如何将数据加载到表单加载、搜索或行中的gridview中,单击“选择所有gridview列到文本框中”,但我对此束手无策。 谢谢。我不确定是否得到了您想要的,但是,您正在使用datagr

经过多次搜索,我来这里寻求帮助。在我的C#winform应用程序中我想创建一个dataGridview在用户在数据库中执行搜索后加载一些记录,一旦用户单击行,它应该在文本框中显示所有记录

我有20多个文本框,很少有combobox和datetimepicker,但我只想在dataGridView中显示几列

我知道如何将数据加载到表单加载、搜索或行中的gridview中,单击“选择所有gridview列到文本框中”,但我对此束手无策。
谢谢。

我不确定是否得到了您想要的,但是,您正在使用datagridview的数据源绑定结果,对吗?如果是这样,在选择数据源后,可以隐藏不想显示的列,如下所示:

DataGridView dgv = new DataGridView();
dgv.DataSource = YourDataTable;
// hide the columns you dont want on grid
dgv.Columns[0].Visible = false;
dgv.Columns[2].Visible = false;
dgv.Columns[3].Visible = false;
dgv.Columns[4].Visible = false;

这样,网格将仅显示所需的列,但当单击事件触发时,您可以访问隐藏列以在控件上显示它们

根据您的评论,下面是如何在datagridview中更改行上的数据。您需要对此进行更改以进行实际的数据库调用。我做了一张小桌子作为例子。为此,创建一个新的winforms应用程序,在其上放置一个DataGridView和两个文本框

using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    DataTable PretendImADataBase;
    public Form1()
    {
        InitializeComponent();

        PretendImADataBase = CreateTestData();
        //this assigns the row enter event to this function.  Each time the row changes,
        //the function is called.  Inside this function, you load your "big data" columns.
        //That way, you only load 2 or 3 columns for all rows, and each time the row changes,
        //you go back out and load all of the details for only that 1 row.
        //Pretty basic way to load data..
        dataGridView1.RowEnter += dataGridView1_RowEnter;
        //initial loading, only 1 or 2 columns of large dataset, to keep loading time fast.
        //primary key, and enough info to identify the full record.            
        dataGridView1.DataSource = CreateDataSource1();
    }        
    private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) 
    {
        //This is where you would make your database call to load big data.
        var x = sender as DataGridView;
        if (x.DataSource == null) return;
        var id = x[0, e.RowIndex].Value;

        DataRow oRow = (from DataRow row in PretendImADataBase.Rows where (int)row["FK"] == (int)id select row).FirstOrDefault();
        if (!(oRow == null))
        {
            textBox1.Text = oRow[3].ToString();
            textBox2.Text = oRow[4].ToString();
        }
    }
    private DataTable CreateTestData()
    {
        DataTable oDt = new DataTable();
        DataColumn oCol = new DataColumn("ID", typeof(int));
        oDt.Columns.Add(oCol);
        oCol = new DataColumn("FK", typeof(int));
        oDt.Columns.Add(oCol);
        oCol = new DataColumn("Data1", typeof(string));
        oDt.Columns.Add(oCol);
        oCol = new DataColumn("Data2", typeof(string));
        oDt.Columns.Add(oCol);

        DataRow oRow = oDt.NewRow();
        oRow["ID"] = 1;
        oRow["FK"] = 1;
        oRow["Data1"] = "Test Data 1";
        oRow["Data2"] = "Test Data 2";
        oDt.Rows.Add(oRow);
        oRow = oDt.NewRow();
        oRow["ID"] = 2;
        oRow["FK"] = 2;
        oRow["Data1"] = "Test Data 3";
        oRow["Data2"] = "Test Data 4";
        oDt.Rows.Add(oRow);
        return oDt;
    }
    private DataTable CreateDataSource1()
    {
        DataTable oDt = new DataTable();
        DataColumn oCol = new DataColumn("ID",typeof(int));
        oDt.Columns.Add(oCol);
        oCol = new DataColumn("Display", typeof(string));
        oDt.Columns.Add(oCol);

        DataRow oRow = oDt.NewRow();
        oRow["ID"] = 1;
        oRow["Display"] = "Test 1";
        oDt.Rows.Add(oRow);
        oRow = oDt.NewRow();
        oRow["ID"] = 2;
        oRow["Display"] = "Test 2";
        oDt.Rows.Add(oRow);
        return oDt;
    }
}
}

上面最简单的方法。将所有数据加载到datagridview中,在不希望显示的列上设置visible=false。然后可以在rowchange事件中访问它们。如果是大数据且性能存在问题,则只能将所需的列加载到datagridview中,然后使用新查询查找有关行更改的其他信息。@Aaron如何使用rowchange事件中的查询在文本框中显示所有数据,结束是这是一个大数据库。你能举个例子吗?是的,给我一些来写点东西。我怎么能补充呢。可见=假;在这个txtbox1.Text=dr[“id”].ToString()中@Bukhalifa是否要隐藏windows窗体控件?尝试txtbox1.Visible=false;谢谢你的帮助,我用更简单的方法解决了。我正在通过使用textchanged event而不是在单元格中单击event将数据加载到gridview,将员工id加载到搜索文本框中,然后在单击“获取数据”按钮后将所有详细信息加载到文本框中。谢谢你的主意