简体   繁体   English

C#Winform一旦单击文本框中的行记录,在gridview中显示几个列

[英]c# winform show few colums in gridview once row click load record in textboxes

after lots of searching i am here to get help. 经过大量搜索后,我在这里获得帮助。 in my C# winform application i want to create a dataGridview to load few records after user performs search in database once user clicks on row it should show all records in textboxes. 在我的C#winform应用程序中,我想创建一个dataGridview以在用户在数据库中执行搜索后加载少量记录,一旦用户单击行,它应该在文本框中显示所有记录。

i have more than 20 texboxes and few combobox and datetimepicker but i just want to show few columns in dataGridView. 我有20多个texbox,很少有combobox和datetimepicker,但是我只想在dataGridView中显示几列。

i know how to load data in to gridview on form load or on search or on row click select all gridview column into textboxes but i am stuck on this. 我知道如何在表单加载或搜索或行上将数据加载到gridview中,然后单击选择所有gridview列到文本框中,但是我对此感到困惑。 Thank You. 谢谢。

Not really sure if I get what you want but, you are using the datasource of the datagridview to bind the results, right? 不确定我能否得到您想要的东西,但是您正在使用datagridview的数据源绑定结果,对吗? if so, after you selected the datasourse, you can hide the columns you dont want to show, like this: 如果是这样,选择数据源后,可以隐藏不想显示的列,如下所示:

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;

This way your grid will show only the columns you want, but when the click event triggers you can access the hidden columns to show them on your controls. 这样,网格将仅显示所需的列,但是在单击事件触发时,您可以访问隐藏的列以在控件上显示它们。

Per your comment, here is how to change data on row changing in a datagridview. 根据您的评论,这是如何在datagridview中的行更改中更改数据。 you will need to alter this to make the actual database calls. 您将需要更改它以进行实际的数据库调用。 I made a small table for example purposes. 我做了一张小桌子作为示例。 For this to work, make a new winforms application, put a DataGridView on it and 2 textboxes 为此,创建一个新的winforms应用程序,在其上放置一个DataGridView和2个文本框

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM