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

Here is my table. patient table

I want the first and last name to be combined as a "name" in a datagridview, how can I do this?

here is my conclusion my conclusion is 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 does not work

+4
source share
3 answers

You simply use the MySQL CONCAT function to combine the two columns and the results into one column with the name. You can use this to display as a grid.

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

Replace it

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

with this

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

Concat function Combine both names with a space

AS FullName (column name) returns this column name

+2

:

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

, .

0

Source: https://habr.com/ru/post/1694896/


All Articles