简体   繁体   English

如何在1列datagridview中显示mysql数据库中的2个不同列

[英]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? 我希望firstname和lastname在datagridview中合并为“name”,我该怎么做?

here is my output My output of datagridview 这是我的输出我的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" ; 我试过这段代码"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. 您只需使用MySQL CONCAT函数将两列和结果连接到一列,如名称所示。 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 Concat功能将两个名称与空间分类相结合

AS FullName(Column Name) return as that Column Name AS FullName(列名称)作为列名返回

Try this: 试试这个:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM