locked
Merging two columns into one column in same datagridview RRS feed

  • Question

  • Hai friends,

    I've a datagridview using C# .net which has been get data fields from MySql and displays like

     

    FirstName    |    LastName   |    ID   

    -------------------------------------------

    Kelvin          |    David         |   001

    Maria           |    John           |   002

    Joe              |    Martin         |   003

     

    This above example shows the datas with 3 fields which is in MySql dataBase Table name "Details"

    I want to display it in my datagridview as like give below

     

    Information   

    --------------

    Kelvin  David  001

    Maria  John    002

    Joe  Martin 003

     

    This is showing in a single field. And it should be with the same table "Details"

    Waiting for Answer...

    Thank you....

    • Moved by edhickey Tuesday, July 20, 2010 3:58 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Tuesday, July 20, 2010 9:00 AM

Answers

  • Hello.

    Please have a look at http://www.dotnetspider.com/resources/4644-Merge-columns-DataGridView-Excel-Spread.aspx

    It allows you to merge any number of columns in a datagridview and display the merged value in a new column.

     


    A marked answer is appreciated.
    • Marked as answer by BU XI - MSFT Tuesday, July 27, 2010 1:18 AM
    Thursday, July 22, 2010 2:26 AM
  • one alternative is you retrive record in datatable and manupulate datatable for merging the data and display in the gridview

    Another is when you retirve from databale retrive record as merged one using sql itself 

    • Marked as answer by BU XI - MSFT Tuesday, July 27, 2010 1:18 AM
    Thursday, July 22, 2010 4:11 AM
  • Hi

    Hope this will helps you

    FirstName    |    LastName   |    ID   

    -------------------------------------------

    Kelvin          |    David         |   001

    Maria           |    John           |   002

    Joe              |    Martin         |   003

    In the Above table you can Merge Columns FirstName and LastName into one column right.

    You can get this using Sql query

    For Example

    Select FirstName + ' '+LastName as Name,ID from Details

    Name                ID

    ---------------------------

    Kelvin David      001

     

    Thanks in Advance

    • Proposed as answer by Arulraj J Thursday, July 22, 2010 5:58 AM
    • Marked as answer by BU XI - MSFT Tuesday, July 27, 2010 1:18 AM
    Thursday, July 22, 2010 5:43 AM
  • //If you just want to merge data for display in datagridview
    
    public SqlConnection GetSqlConn()
    {
      string M_str_sqlcon = "Server=.;Database=Northwind;User id=sa;PWD=";
      SqlConnection mycon = new SqlConnection(M_str_sqlcon);
      mycon.Open();
      return mycon;
    }
    
        public DataSet GetDataSet(string sqlComm, string tableName)
        {
          SqlConnection sqlconn = this.GetSqlConn();
          SqlDataAdapter sqlda = new SqlDataAdapter(sqlComm, sqlconn);
          DataSet ds = new DataSet();
          sqlda.Fill(ds, tableName);
          return ds;
        }
    
        private void frmMain_Load(object sender, EventArgs e)
        {
          ds = db.GetDataSet("SELECT (FirstName+' '+LastName+' '+ID) AS Detail FROM Table", "Table");
          datagridview.DataSource = ds.Tables[0];
         }
    

    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#
    • Proposed as answer by Arulraj J Thursday, July 22, 2010 5:58 AM
    • Marked as answer by BU XI - MSFT Tuesday, July 27, 2010 1:18 AM
    Thursday, July 22, 2010 5:49 AM

All replies

  • Hello.

    Please have a look at http://www.dotnetspider.com/resources/4644-Merge-columns-DataGridView-Excel-Spread.aspx

    It allows you to merge any number of columns in a datagridview and display the merged value in a new column.

     


    A marked answer is appreciated.
    • Marked as answer by BU XI - MSFT Tuesday, July 27, 2010 1:18 AM
    Thursday, July 22, 2010 2:26 AM
  • one alternative is you retrive record in datatable and manupulate datatable for merging the data and display in the gridview

    Another is when you retirve from databale retrive record as merged one using sql itself 

    • Marked as answer by BU XI - MSFT Tuesday, July 27, 2010 1:18 AM
    Thursday, July 22, 2010 4:11 AM
  • Hi

    Hope this will helps you

    FirstName    |    LastName   |    ID   

    -------------------------------------------

    Kelvin          |    David         |   001

    Maria           |    John           |   002

    Joe              |    Martin         |   003

    In the Above table you can Merge Columns FirstName and LastName into one column right.

    You can get this using Sql query

    For Example

    Select FirstName + ' '+LastName as Name,ID from Details

    Name                ID

    ---------------------------

    Kelvin David      001

     

    Thanks in Advance

    • Proposed as answer by Arulraj J Thursday, July 22, 2010 5:58 AM
    • Marked as answer by BU XI - MSFT Tuesday, July 27, 2010 1:18 AM
    Thursday, July 22, 2010 5:43 AM
  • //If you just want to merge data for display in datagridview
    
    public SqlConnection GetSqlConn()
    {
      string M_str_sqlcon = "Server=.;Database=Northwind;User id=sa;PWD=";
      SqlConnection mycon = new SqlConnection(M_str_sqlcon);
      mycon.Open();
      return mycon;
    }
    
        public DataSet GetDataSet(string sqlComm, string tableName)
        {
          SqlConnection sqlconn = this.GetSqlConn();
          SqlDataAdapter sqlda = new SqlDataAdapter(sqlComm, sqlconn);
          DataSet ds = new DataSet();
          sqlda.Fill(ds, tableName);
          return ds;
        }
    
        private void frmMain_Load(object sender, EventArgs e)
        {
          ds = db.GetDataSet("SELECT (FirstName+' '+LastName+' '+ID) AS Detail FROM Table", "Table");
          datagridview.DataSource = ds.Tables[0];
         }
    

    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#
    • Proposed as answer by Arulraj J Thursday, July 22, 2010 5:58 AM
    • Marked as answer by BU XI - MSFT Tuesday, July 27, 2010 1:18 AM
    Thursday, July 22, 2010 5:49 AM
  • Hei,

         Thanks buddy, I got it..... :-)

    Friday, July 23, 2010 5:48 AM