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!

Datagrid column alignment question

Status
Not open for further replies.

Trebor100

Programmer
Mar 14, 2006
90
GB
Hi all.

Got a stupid problem here and was hoping someone could point at me, laugh, and tell me what i've done wrong. The problem is a simple one. I need to set the Places Remaining column to align right in the following code:

DataTable myDataTable = new DataTable();
DataRow myDataRow;
TrainDB mySP = new TrainDB();
SqlDataReader myReader = null;

myDataTable.Columns.Add(new DataColumn("Date", typeof(string)));
myDataTable.Columns.Add(new DataColumn("Title", typeof(string)));
myDataTable.Columns.Add(new DataColumn("Duration\n(Days)", typeof(string)));
myDataTable.Columns.Add(new DataColumn("Start", typeof(string)));
myDataTable.Columns.Add(new DataColumn("Finish", typeof(string)));
myDataTable.Columns.Add(new DataColumn("Places Remaining", typeof(int)));

while (myReader.Read())
{
try
{
if (Convert.ToInt32(myReader.GetValue(5)) - Convert.ToInt32(myReader.GetValue(6)) > 0)
{
//Create a new data row in the data table
myDataRow = myDataTable.NewRow();
myDataRow[0] = DateTime.Parse(myReader.GetValue(0).ToString()).ToShortDateString();
myDataRow[1] = myReader.GetValue(1);
myDataRow[2] = myReader.GetValue(2);
myDataRow[3] = DateTime.Parse(myReader.GetValue(3).ToString()).ToShortTimeString();
myDataRow[4] = DateTime.Parse(myReader.GetValue(4).ToString()).ToShortTimeString();
myDataRow[5] = Convert.ToInt32(myReader.GetValue(5)) - Convert.ToInt32(myReader.GetValue(6));

myDataTable.Rows.Add(myDataRow);
}
}
catch{

}
}
myReader.Close();

DataView myDataView = new DataView(myDataTable);
myDataGrid.DataSource = myDataView;
myDataGrid.AllowPaging = true;
myDataGrid.PagerStyle.Mode = PagerMode.NumericPages;
myDataGrid.PageSize = 15;
myDataGrid.AllowSorting=false;
myDataGrid.HeaderStyle.CssClass="thstyle";
myDataGrid.PagerStyle.CssClass="pgstyle";
myDataGrid.PagerStyle.NextPageText="Next Page";
myDataGrid.PagerStyle.PrevPageText="Previous Page";
myDataGrid.AlternatingItemStyle.CssClass="gridalt";
myDataGrid.CssClass="gridstyle";
myDataGrid.DataBind();
lblPage.CssClass="currentpage";
lblPage.Text = "Page No: " + (myDataGrid.CurrentPageIndex + 1);
Session["Source"] = myDataTable;


I've done the myDataGrid.Column[5].HorizontalAlignment but the problem is the column collection is zero when i try and do it... what am i missing?

cheers all
Rob
 
Are you autogenerating columns in the DataGrid? When dataGrid columns are auto generated, there is no column collection as far as I am aware.

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Hi Rhys - Yes i am autogenerating the columns and from playing around with this it would appear that there is no column collection when this is done.... is there anyway around this?

ow and i love the use of COBOL line, how true that is.
 
:) Ah, Cobol...

I've not found a way around it while autogenerating columns, but for a long time now the only thing I've used autogenerating columns for is when uploading an excel sheet that requires visual verification prior to popping into a dB.

If possible I'd look at restructuring your architecture a little, but how really depends on the scope of the project you're working on, and whether you're .Net 1.0/1.0 or .net 2.0.

My initial thoughts looking just at the posted code would be;
You're creating a lot of overhead in the way you're populating your DataSource for your DataGrid. Have a look at the DataAdaptor classes, (i.e., OleDbDataAdapter), and utilising this to populate straight into a DataSet and DataTable.
When setting your DataSource, I see you're not filtering or utilising any other DataView methods, you're just using it as a straight DataSource. Look into what objects can be a DataSource for a DataGrid. Try using your DataTable as the DataSource without being wrapped in a DataView unless you really need to.
Also, look deeper into DataBinding with DataGrids and DataSets, and utilising BoundColumns.

Hope this helps :)

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Cheers for the pointers Rhys. Thats going to be the job for today
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top