简体   繁体   中英

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

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

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

Try this:

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

Hope this helps.

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