DataGrid formatting not working

KnightHasan

Newcomer
Joined
Apr 28, 2005
Messages
6
Hi all! I have the following three questions, the answers to which still elude me. :confused: Any help would be highly appreciated:

1. I have a DataGrid (firstly, with a disabled viewstate and secondly, the entire DataGrid is editable, with a single Button at the bottom instead of editing data one row at a time). This DataGrid displays rows from a table A in an Oracle database. When the Submit button is clicked, it will result in two actions:
(a) A csv file will get downloaded to the user's computer which will contain all the rows of the grid and at the same time these rows will be inserted into another table B.
(b) The entire grid will be recreated and reloaded. During the reload all the original rows will be loaded with the difference that since they have been downloaded to the user's computer, they will be colored differently(For this a query checks the table B). I have used OnItemDataBound event for this.
The problem is in step (b) during the reloading of data from the datasource. Although the code does reach the lines which color the rows (in OnItemDataBound event handler) and they get executed, yet when the page gets displayed finally, the rows appear with the original color. The change comes into effect only if I refresh the page through the browser.
Why is this happening and how can I make sure that the rows do get colored when the datagrid is reloading?

2. This is related to step 1. I want the background of the rows to get colored. For this I use the statement:
Code:
e.Item.BackColor = Color.DodgerBlue;
where e is of type DataGridItemEventArgs. Yet this does not work. But strangely, the forecolor does change using :
Code:
e.Item.ForeColor = Color.DodgerBlue;
How can I change the BackColor?

3. Can I disable a custom GridRadioButtonListColumn inside a DataGrid so that selected radio is visible to the user but cannot be deselected/modified?

Thanks.
 
1 You need to re-load the grid in the event handler of the "single Button at the bottom of the grid" and ensure that your load routine in the page_load is done only when "Not IsPostBack"

2 it should work once step 1 works.

3 you can make the Radio buttons Read-Only
 
Hi Robby,

Thx for the reply. And I am extremely sorry for the delay in sending my reply. This was owing to my office being shifted. I will get back to you shortly.
 
Hey there Robby,

I finally got the chance to try out ur suggestions.

1)
You need to re-load the grid in the event handler of the "single Button at the bottom of the grid" and ensure that your load routine in the page_load is done only when "Not IsPostBack"

The re-load is being done in the said event handler and also the laod-routine in page-load is done onle when !IsPostBack.
I cross-checked this and it is not working

2) Ditto

3) For the Radio buttons, they are getting disabled when I use IE as the browser but it was not working in Netscape(which I was using earlier)

So, as regards points 1 & 2, I am back to square one. Kindly do help me out if anybody has a solution.
 
The code...

Hi Robby, first of all thanx for taking active interest in my problem. Here is the relevant code from my application:

Code:
private void vend_csv_Click(object sender, System.EventArgs e)
{
	DownloadCSVFile();
}

private void DownloadCSVFile()
{
	int aTripCount = 0;
	string aDownLoadFileContent = [COLOR=DarkRed]GetDownloadFileAsString[/COLOR](ref aTripCount);
	if( aDownLoadFileContent.Length==0 )
	{
		tripsCtl.Toolbar.DisplayMessage( "Nothing to download" );
		return;
	}
			
	string aDateFormat = DateTime.Now.ToString("_MMddyyyy_HHmm");
             string aDownLoadFileName = userID + aDateFormat + ".CSV";  	
	string aDownLoadFileSize = aDownLoadFileContent.Length.ToString();

	MessageBox.Show( "About to download " + aTripCount + " Trip(s) for today ...", "My App", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
	[COLOR=DarkRed]CreateGrid();[/COLOR]
			
	//to force the browser's download dialog
	Response.AppendHeader("Content-Type", "application/x-msdownload");
	Response.AppendHeader("Content-Length", aDownLoadFileSize);
	Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + aDownLoadFileName + "\"; size=" + aDownLoadFileSize);

	Response.Write( aDownLoadFileContent );
}

protected void OnGridItemDataBoundEvent( object sender, DataGridItemEventArgs e )
{
	//Color rows containing downloaded trips with a different color AND disable Yes/No for "trip acceptance" radiobutton column
	if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
	{
		DataRowView dataRowVw = (DataRowView)e.Item.DataItem;
		string tripID = (string)dataRowVw["tripid"];
		int aDownloadedTripExists = [COLOR=DarkRed]Trips.FindDownloadedTrips[/COLOR]( dataRowVw["tripid"] as string);
			
		if (aDownloadedTripExists == 1)
		{
			e.Item.ForeColor = Color.DodgerBlue;
			//e.Item.BackColor = System.Drawing.Color.LightGray;
			
			e.Item.Cells[2].Enabled = false;
		}
	}
}

This is an ASP.Net application using C# and Oracle 9i as backend. All the above functions are on the same aspx page.

In the above lines of code, "vend_csv_Click" is the submit button event handler.
"DownloadCSVFile()" is the function which creates the csv file. It further contains calls to two more functions : "GetDownloadFileAsString()" and "CreateGrid()".
"GetDownloadFileAsString()" is the function which creates the string which will be the content of the downloaded file (It makes use of the StringBuilder class).
"CreateGrid()" is the actual function where the DataGrid is reloaded from the database.
"Trips.FindDownloadedTrips" is a function ("Trips" is an Oracle package) which checks the existance of a particular trip in a DB table and returns 1 if found or 0 if not.

As I said earlier, though the code does reach the lines which color the rows (in OnGridItemDataBound event handler) and they get executed, yet when the page gets displayed finally, the rows appear with the original color. The change comes into effect only if I refresh the page through the browser. Also, the ForeColor is working but not the BackColor(which is why that particular line is commented in "OnGridItemDataBoundEvent()").
 
Last edited:
You did not do the my first step "You need to re-load the grid in the event handler of the "single Button at the bottom of the grid" , the only thing you're doing in the submit button is calling DownloadCSVFile();
 
Robby said:
You did not do the my first step "You need to re-load the grid in the event handler of the "single Button at the bottom of the grid" , the only thing you're doing in the submit button is calling DownloadCSVFile();

OK. What I have done now is as follows:

Code:
private void vend_csv_Click(object sender, System.EventArgs e)
{
	DownloadCSVFile();
             CreateGrid();
}

And I removed the "CreateGrid()" call from inside the "DownloadCSVFile()".

Sadly, the grid does get reloaded there and then, but the individual rows of the downloaded trips do not get colored.
What might be going wrong? :confused:
 
Robby said:
sorry, I didn't notice "CreateGrid()" in DownloadCSVFile....

OK. So what next. Kindly forward any suggestions that you have. I need to correct this as soon as possible and I have browsed tens of sites for a solution but, alas, it still eludes me. :(

All suggestions would be highly appreciated.
 
Back
Top