简体   繁体   中英

Data will not display in textbox using StringBuilder C#

Anyone with some C# experience that could help would be greatly appreciated. When I debug the code it shows that my data access function is indeed pulling the correct data, but it just will not display in my text box. FYI this is a multi-line text box so I am trying to start a new line after each item. My functions are below:

public class BuildersFirstSource
{
    public string sPlant { get; set; }
    public string sItem { get; set; }
    public string sCustomerNumber { get; set; }
    public string sDescription { get; set; }
    public DateTime dtInvoiceDate { get; set; }
    public decimal dUnitPrice { get; set; }
    public decimal dQuantityBuilt { get; set; }
    public decimal dTotalPrice { get; set; }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append(sPlant).Append(",");
        sb.Append(sItem).Append(",");
        sb.Append(sCustomerNumber).Append(",");
        sb.Append(sDescription).Append(",");
        sb.Append(dtInvoiceDate).Append(",");
        sb.Append(dUnitPrice).Append(",");
        sb.Append(dQuantityBuilt).Append(",");
        sb.Append(dTotalPrice).Append(",");

        return sb.ToString();
    }
}

private void btnSubmit_Click(object sender, EventArgs e)
{
    string sMessage = "";
    List<BuildersFirstSource> lstBuilders = new List<BuildersFirstSource>();
    BuildersFirstSource builder = new BuildersFirstSource();

    if (DataAccess.GetPriceOne(lstBuilders, out sMessage) == false)
    {
        MessageBox.Show(sMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    FillPriceData(lstBuilders);
}

private void FillPriceData(List<BuildersFirstSource> lstBuilders)
{
    StringBuilder sHTML = new StringBuilder();

    foreach (BuildersFirstSource item in lstBuilders)
    {
        sHTML.Append(lstBuilders);
        sHTML.Append(Environment.NewLine);
    }
    //txtPriceOne.Text = sHTML.ToString();
}

You seem to be adding the whole list of items to the string builder for each item in the lstBuilders collection. You should be adding the field you are interested in displaying:

foreach (BuildersFirstSource item in lstBuilders)
{
    sHTML.AppendLine(item.TheFieldYouAreInterestedIn.ToString());
}
txtPriceOne.Text = sHTML.ToString();

You didn't show what BuildersFirstSource is exactly, and the line that populates your TextBox control is commented out, but shouldn't your code look more like this?

StringBuilder sHTML = new StringBuilder();

foreach (BuildersFirstSource item in lstBuilders)
{
    // Modify the following line so that you are adding the
    // correct string from item
    sHTML.AppendLine(item.ToString());
}
txtPriceOne.Text = sHTML.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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