简体   繁体   中英

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.

i have more than 20 texboxes and few combobox and datetimepicker but i just want to show few columns in 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. 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? 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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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