Lyndon Broz Tonelete
Lyndon Broz Tonelete

Reputation: 163

How to combine fields in datagridview from Sql field names in c#

I have a DatagridView control that has 4 columns.

datagridview with 4 columns

I want the FirstName, MiddleName and LastName to be merged and call it as FullName.

Like this :

FullName

My codes as of now is :

    public void Data()
    {
        _da = new SqlDataAdapter();
        _da.SelectCommand = cmd;
        DataTable _dt = new DataTable();
        _da.Fill(_dt);
        BindingSource bs = new BindingSource();
        bs.DataSource = _dt;
        PresDG.DataSource = bs;
        _da.Update(_dt);
    }

    private void President()
    {
        sc.Open();
        cmd = new SqlCommand("SELECT * FROM TableVote WHERE Position='President'", sc);

        try
        {
            Data();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }
    }

How do I do it in my Sql Statement? Thanks in advance :)

Upvotes: 0

Views: 437

Answers (1)

Saghir A. Khatri
Saghir A. Khatri

Reputation: 3128

try using

cmd = new SqlCommand("SELECT FirstName+' '+MiddleName+' '+LastName as FullName,VCount FROM TableVote WHERE Position='President'", sc);

Upvotes: 3

Related Questions