user8647100
user8647100

Reputation:

How to display 2 different column from mysql database in 1 column datagridview

Here is my table. table patient

I want firstname and lastname to be combined as "name" in datagridview, how can i do this?

here is my output My output of datagridview

And my code..

private void frmPatient_Load(object sender, EventArgs e)
    {

        MySqlConnection con = new MySqlConnection("server = localhost; database = nuclinic; username = root; password = ; Convert Zero Datetime=True");

        string query = "select firstname, lastname from patient";

        using (MySqlDataAdapter adpt = new MySqlDataAdapter(query, con))
        {

            DataSet dset = new DataSet();

            adpt.Fill(dset);

            dataGridView1.DataSource = dset.Tables[0];

        }
        con.Close();
    }

I tried this code "SELECT firstname + ', ' + lastname AS name"; but it's not working

Upvotes: 4

Views: 594

Answers (3)

Amit Singh
Amit Singh

Reputation: 77

You just use the MySQL CONCAT function to concatenate two columns and results into one column as given as name. You can use this to display in the grid view.

select   CONCAT(firstname,' ', lastname) as name, firstname, lastname from patient

Upvotes: 2

Okasha Rafique
Okasha Rafique

Reputation: 772

Replace this

string query = "select firstname, lastname from patient";

with this

string query = "select CONCAT(firstname," ",lastname) as FullName from Patient";

Concat Function Combine both name with space sperated

AS FullName(Column Name) return as that Column Name

Upvotes: 2

onhax
onhax

Reputation: 339

Try this:

select CONCAT(firstname," ",lastname) as Name from Patient

Hope this helps.

Upvotes: 0

Related Questions