Datagrid cell properties manipulation

J

js

Question 1:
I have the columns autogenerate = false and add each column
programatically. One of the column is bound to a integer field (Due)
of a recordset. I am trying to change the content of the cell depending
on the value of the bound field. I need to use the following rules to
set the content of the cell. I would appreciate anyone filling the code
comments for needed code. Thanks a million.

private void MyDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
....
....
if ((itemType == ListItemType.AlternatingItem) || (itemType ==
ListItemType.Item))
{
if (strRequester == "Requester1")
{
intDue = (Int32)((DataRowView)e.Item.DataItem)["Due"];
if (intDue < 0)
{
e.Item.ForeColor = Color.White;
if
(((DataRowView)e.Item.DataItem)["SystemDeployed"].GetType().ToString()
== "System.DateTime")
{
e.Item.BackColor = Color.Green;
//need code to set cell content to display "Completed" here...
}
else
{
e.Item.BackColor = Color.Crimson;
//need code to set cell content to display "Over Due" here...
}
}
if (intDue >= 0 && intDue <= 5)
{
e.Item.BackColor = Color.Khaki;
//need code to set cell content to display "Due Soon" here...
}
}
....
....
}

Question 2:
I need to set the width of a column that is bound to a SQL ntext field,
so that this column is always a fixed width diregarding the length of
the context length. I tried the flowing code which is called in the
InitializeComponent(), but the text column width fluctuates.

private void addBoundColumn(string strDataField, string
strHeaderText, string strFieldType)
{
BoundColumn bc = new BoundColumn();
bc.HeaderText = strHeaderText;
bc.HeaderStyle.CssClass = "listHeaderStyle";
bc.HeaderStyle.ForeColor = Color.White;
bc.Visible = true;
bc.DataField = strDataField;
switch(strFieldType)
{
case "Text":
//no sorting on text field
bc.HeaderStyle.Width = Unit.Pixel(100);
bc.ItemStyle.Width = Unit.Pixel(100);
break;
case "DateTime":
bc.SortExpression = strDataField;
bc.DataFormatString = "{0:d}";
break;
default:
bc.SortExpression = strDataField;
break;
}
MyDataGrid.Columns.Add(bc);
}
 
G

Guest

Hi JS,

Suppose it's BoundColumn, you can assign text to it:

e.Item.Cells[col_index].Text = "Completed";

HTH

Elton Wang
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top