hi,

i am using VS standard 2008 and a sqlclient file,
i want to get two fields from the database(same table) and merge it (as firstname lastname) and display in the datagridview combo column in the datagrid view,
how can i do this,

please can someone help me
thanxx

Dani AI

Generated

As pointed out, ADO.NET is the right tool here. Two practical patterns work well for showing "FirstName LastName" in a DataGridViewComboBoxColumn: (1) return a computed FullName from the SQL query (best for performance and sorting), or (2) add a computed column to the DataTable after filling it. When either name can be NULL, prevent NULL results by using ISNULL (SQL) or IsNull (DataTable expression).

Example SQL (returns FullName directly):

SELECT Id,
       ISNULL(FirstName, '') + ' ' + ISNULL(LastName, '') AS FullName
FROM   YourTable

Example VB.NET binding pattern (fill DataTable, bind to combo column):

Dim dt As New DataTable()
Using cn As New SqlConnection(connectionString)
    Using da As New SqlDataAdapter(sql, cn)
        da.Fill(dt)
    End Using
End Using

Dim combo As New DataGridViewComboBoxColumn()
combo.DataSource = dt
combo.DisplayMember = "FullName"
combo.ValueMember = "Id"
combo.DataPropertyName = "Id"  ' bind to the grid's value column if needed
DataGridView1.Columns.Add(combo)

Alternative (compute in the DataTable):

dt.Columns.Add("FullName", GetType(String), "IsNull(FirstName, '') + ' ' + IsNull(LastName, '')")

Common pitfalls: ensure DisplayMember and ValueMember names match columns in the combo DataSource; ensure every row's ValueMember exists in the combo source (otherwise "value is not valid" appears); prefer SQL-side concatenation for large datasets. This approach fits the VS2008/SqlClient scenario described by .

Recommended Answers

All 2 Replies

>how can i do this

Using ADO.NET class library.

can u tell me how??
thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.