English 中文(简体)
How to bind dataGridView predefined columns with columns from sql statement (without adding new columns)?
原标题:

Is there a elegant way, to bind predefined dataGridView columns with results from a SQL statement?

Example:

dataGridView1.Columns.Add("EID", "ID");
dataGridView1.Columns.Add("FName", "FirstName");

Some SQL like

SELECT t.FirstName AS FName, t.EmpID AS EID 
FROM table t ...

and then I call

 dataGridView1.DataSource = someDataSet.Tables[0].DefaultView;

The last call add columns to my datagrid but I just want to bind it by column name not to add new columns.

The example will give a result like this:

Table columns: ID, FirstName, FName, EID (ID and FirstName holds empty cells)

How to get this:

Table columns: ID, FirstName or FirstName, ID

Best regards!

问题回答

Use dataGridView1.Columns["FName"].DataPropertyName = "FName" where FName is column in your data table.

Aside from setting AutoGenerateColumns to false, you also need to set DataPropertyName for each column in the DataGridView to the corresponding field in the data source. You can set this in the designer or in code before setting the DataSource property.

I think the DataGridView has an AutoGenerateColumns property, doesn t it?

dataGridView1.AutoGenerateColumns = True;

From the MSDN docs:

public bool AutoGenerateColumns { set; get; } Member of System.Windows.Forms.DataGridView

Summary: Gets or sets a value indicating whether columns are created automatically when the System.Windows.Forms.DataGridView.DataSource or System.Windows.Forms.DataGridView.DataMember properties are set.

Returns: true if the columns should be created automatically; otherwise, false. The default is true.

The property isn t on the Properties window though, you have to set it via code as in my example.

If you are doing WinForm, the important part is setting the DataPropertyName property to match the DataTable Column name. You can do it in the designer or code as follows:

Me.AccountDataGridView.Columns("Account").DataPropertyName = "Account"

Of course, having set this:

Me.AccountDataGridView.AutoGenerateColumns = False

How about adding columns to the Columns tag of your gridview like so?

<Columns>
<asp:BoundField DataField="EID" HeaderText="ID" />
<asp:BoundField DataField="FName" HeaderText="First name" />
...




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签