none
How to insert space b/n dataset Columns? RRS feed

  • Question

  • Hi all,

    I am working on a project in C#. I have a dataset called 'ds' having columns. I want to have another column by concatenating two of the current columns. At the same time I want to have spaces like two or three in between them like 'X     Y'. How can I achieve this?

    Here is the line of code in my application and I want to put more than one space b/n "Supplement" and   "ClusterAmount" like: "Supplement"      "ClusterAmount".  

    ds.Tables[0].Columns.Add("ClustersAmt", Type.GetType("System.String"), "Supplement + ' ' + ClusterAmount");

    Thank you in advance.

     

    Monday, August 13, 2007 1:30 PM

Answers

  • Nothing prevents you to concatenate with multiple spaces like (for example 10)

     

    MyDataTable.Column.Add(New DataColumn("MyNewColumnName", System.Type.GetType("System.String"),"Supplement + '" & New String(Chr(20),10) & "' + ClusterAmount'))

    Tuesday, August 14, 2007 10:19 PM
    Moderator

All replies

  • Do youwant space between values or what.

     

    Regards,

    Wasif Ahmad

    Monday, August 13, 2007 3:49 PM
  • Thank you for your response.

    What I was looking is to have space between "Supplement" and "ClusterAmount" Columns being concatenated like :

    ("Supplement"   [space]     "ClusterAmount") for dispaly purpose on listbox control.

    So any clue?

    Thank you.

     

     

    Monday, August 13, 2007 4:37 PM
  • Dear,

     

    Can you send me code. So that i will fix it andgive it back to you.

     

    Regards,

    Wasif Ahmad

     

    Monday, August 13, 2007 4:46 PM
  •  

    Here is the Code.

    cClustersAmt is a ListBox control and the ClusterAmt column is a result of "Supplement + ' ' + ClusterAmount");.

    I wanna have space (More than two) between these columns upon concatination.Right now I have only one space.

    Thank you for any replay.

     

    Fund_bus cluBusObj= new Fund_bus();

    DataSet clusterAmt = cluBusObj.GetClusterAmount(fy,Number);

    ds.Tables[0].Columns.Add("ClustersAmt", Type.GetType("System.String"), "Supplement + ' ' + ClusterAmount");

    if ((int)clusterAmt.Tables[0].Rows.Count > 0)

    {

    cClustersAmt.DataSource = clusterAmt;

    cClustersAmt.DataTextField = "ClustersAmt";

    cClustersAmt.DataValueField = "ClustersAmt";

    cClustersAmt.DataBind();

    }

     

    Monday, August 13, 2007 4:56 PM
  • Dear,

     

    Here is the code which might help you.

     

    Dim ds As New DataSet

    Dim dt As New DataTable("Tab")

    ds.Tables.Add(dt)

    ds.Tables(0).Columns.Add("Supplement", Type.GetType("System.String"))

    ds.Tables(0).Columns.Add("ClusterAmount", Type.GetType("System.String"))

    ds.Tables(0).Columns.Add("ClustersAmt", Type.GetType("System.String"))

    Dim dr As DataRow

    dr = dt.NewRow

    dr("Supplement") = "Supplement"

    dr("ClusterAmount") = "ClusterAmount"

    dt.Rows.Add(dr)

    ds.Tables(0).Rows(0).Item("ClustersAmt") = ds.Tables(0).Rows(0).Item("Supplement").ToString & " - " & ds.Tables(0).Rows(0).Item("ClusterAmount").ToString

    dr = dt.NewRow

    dr("Supplement") = "1"

    dr("ClusterAmount") = "2"

    dt.Rows.Add(dr)

    ds.Tables(0).Rows(1).Item("ClustersAmt") = ds.Tables(0).Rows(1).Item("Supplement").ToString & " - " & ds.Tables(0).Rows(1).Item("ClusterAmount").ToString

    lstBox.DataSource = ds

    lstBox.DataTextField = "ClustersAmt"

    lstBox.DataValueField = "ClustersAmt"

    lstBox.DataBind()

     

    Regards,

    Wasif Ahmad

    Monday, August 13, 2007 5:20 PM
  • You could use expression column for this purpose. Expression columns allow using some functions to get result from other columns. To create column you need your code would look like

     

    MyDataTable.Column.Add(New DataColumn("MyNewColumnName", System.Type.GetType("System.String"),"Supplement + ' ' + ClusterAmount'))

     

    It will create new column where value will be concatenation of two columns and space. If ClusterAmount is numeric, you might need to use CONVERT function inside of the expression like

     

    MyDataTable.Column.Add(New DataColumn("MyNewColumnName", System.Type.GetType("System.String"),"Supplement + ' ' + CONVERT(ClusterAmount,'System.String')'))

     

    Tuesday, August 14, 2007 10:40 AM
    Moderator
  • Thank you very much for your replay,

    I have this line of code and it is working. But my question was to have more that one space b/n the two fields being concatinated.

    Any Clue?

    Thnak you.

    Tuesday, August 14, 2007 6:23 PM
  • Nothing prevents you to concatenate with multiple spaces like (for example 10)

     

    MyDataTable.Column.Add(New DataColumn("MyNewColumnName", System.Type.GetType("System.String"),"Supplement + '" & New String(Chr(20),10) & "' + ClusterAmount'))

    Tuesday, August 14, 2007 10:19 PM
    Moderator
  • Is it a good idea to create new string column concatenated with other 2 string columns? It spoils first normal form!

    Saturday, November 3, 2007 8:26 AM
  •  grzesiek_w wrote:

    Is it a good idea to create new string column concatenated with other 2 string columns? It spoils first normal form!

    No, this should be implemented in the business layer or the user interface layer. But sometimes you need to denormalize for performace, and this kind of logic can be pushed into stored procedures.

     

    For the OP's question, I think create a strong typed dataset and extend the strong typed datarow with a calculated property in a partial class will do.

    Saturday, November 3, 2007 3:37 PM