Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do not remove/skip white space in GridView 1

Status
Not open for further replies.

chilly442

Technical User
Jun 25, 2008
151
US
I am attempting to build a text report from data that is loaded into a GridView.
The GridView is populated from a DataTable which is filled with data from a query.

Each column is required to be a fixed number of spaces or characters.

The problem that I am having is there are a few columns in the DataTable that contain
white space place holders where data in the text report is not available or is not needed.
I still need to keep the white space in there so that the report stays in the correct format.

When I build the DataTable, I can view it and see that the required columns contains the white space that I need.

When I pass the data from the DataTable to the GridView, the GridView ignores or deletes the white space,
and the format for the report is now incorrect.

Example:

@ = white space

DataTable
C1 C2 C3 C4 C5
----------------------------------
01 01 2012 @@@@ 01
01 02 2012 @@@@ 01



GridView
C1 C2 C3 C4 C5
----------------------------------
01 01 2012 01
01 02 2012 01

I need to know if there is a way to tell the GridView not to delete the white space?
Or if there is a better way to do what I am trying to do.

Thanks,
Chilly442
---------------------------------------
 
Do you know how many spaces are in the column(s) that contain white space?
If so, in your query, just replace the white space chars with and "nbsp;" for each space. When the grid view renders, it will show each "nbsp&" as a space.
 
So I loop through the dtEvents DataTable and get the values that I need. I do some checking, which I have left out.
Then I build the dtMaster_Events DataTable and bind that to the Grid.

I tried:
sC4 = new String(' ', 20);
//===

And a few variations of:
String sSpace = " ";
sC4 = sSpace + sSpace + sSpace + sSpace;
//===
But I either get no spaces in the column or I get the literal "   &nbsp"


foreach (DataRow dr in dtEvents.Rows)
{
sC1 = Convert.ToString(dr["RECORD_CODE"]);
sC2 = Convert.ToString(dr["UTILITY_CODE"]);
sC3 = Convert.ToString(dr["YEAR"]);
sC5 = Convert.ToString(dr["ROW_ID"]);

sC1 = "01";
sC2 = "01";
sC3 = "2012";
sC4 = "@@@@"; //This should be 4 spaces//and the data does not come from the Query//It is just a place holder
//That is part of the report but not part of the data from the query//
sC5 = "01";

myNewRow = dtMaster_Events.NewRow();

if (dtMaster_Events.Rows.Count == 0)
{
myNewRow.Table.Columns.Add("C1");
myNewRow.Table.Columns.Add("C2");
myNewRow.Table.Columns.Add("C3");
myNewRow.Table.Columns.Add("C4");
myNewRow.Table.Columns.Add("C5");
}


myNewRow["C1"] = sC1;
myNewRow["C2"] = sC2;
myNewRow["C3"] = sC3;
myNewRow["C4"] = sC4;
myNewRow["C5"] = sC5;

dtMaster_Events.Rows.Add(myNewRow);

}

GridView1.DataSource = dtMaster_Events;
GridView1.DataBind();

Thanks for taking the time to help me out.


Thanks,
Chilly442
---------------------------------------
 
I use a utility function that generates a string of repeating characters. In this case "&nbsp; I then places that in a "<td>".
The table I create is generated in a stringbuilder. This should still work for you:
Code:
'Code snippet:
 VideoDetails.AppendFormat("<td style='text-align: left;'>{0}{1}{2}", [COLOR=red][b]RepeatString("&nbsp;", 10)[/b][/color] + "Title:&nbsp;&nbsp;", Videos(k)("Title"), "<br />")
..more code here

[COLOR=red][b]Private Function RepeatString[/b][/color](ByVal strToRep As String, ByVal noOfRepeats As Integer) As String
        Dim str As New StringBuilder
        Dim x As Int16

        For x = 1 To noOfRepeats
            str.Append(strToRep)
        Next x

        Return str.ToString
    End Function
This places the correct number of white space chars when the table is rendered. If id does not work in the gridview, there may be a property you need to set to not have it trim the white space.
 
I had a look around and couldn't come up with a way to keep the grid from doing a trim.

I'll give your solution a shot and see what I can come up with.

Thanks for the help!

Thanks,
Chilly442
---------------------------------------
 
I was able to take what you suggested above and make it work. I just placed the code in the Page_Load Event, and then call it inside the RowDataBound Event.

There is probably a better way to do this (and a cleaner way) but this will work for now.

Code:
StringBuilder str = new StringBuilder(" ",20);

protected void Page_Load(object sender, EventArgs e)
{
     for (int i = 1; i <= 20; i++)
     {
          str.Append("&nbsp;");
     }
}

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
     foreach (GridViewRow gr in GridView1.Rows)
     {
          gr.BackColor = System.Drawing.Color.Silver;
          gr.Cells[4].Text = Convert.ToString(str);
     }
}

I attempted to use a StringBuilder but I didn't have this part correct so it didn't work correctly.
str.Append("&nbsp;");

Thank you for taking the time to help me and posting some code to show me what I was doing wrong.


Thanks,
Chilly442
---------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top