我有超过20个texbox和几个组合框和datetimepicker,但我只想在dataGridView中显示几列。
我知道如何在表单加载或搜索时将数据加载到gridview或在行上单击选择所有gridview列到文本框但我仍然坚持这一点。 谢谢。
答案 0 :(得分:0)
不确定我是否得到了你想要的东西但是,你正在使用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;
这样,您的网格将只显示您想要的列,但是当点击事件触发时,您可以访问隐藏的列以在控件上显示它们。
答案 1 :(得分:0)
根据您的评论,以下是如何在datagridview中更改行更改的数据。您需要更改它以进行实际的数据库调用。我做了一个小桌子作为例子。为此,创建一个新的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;
}
}
}