Displaying custom values in the cell
hi,
Im using a winforms datagrid to display values for a datatable.
I want to show a different data type (value) other than the value in the datasource like if the datatable contains a value 1 in one column , i should show a mapped string in the datagrid something like 1- critical , 2 - normal ,like when the source contains 1 , the datagrid should show "critical" instead of 1.
Could anyone give me ideas of how can i format the data before it is displayed on the datagrid
Thanks
vinothkumar
[507 byte] By [
vinxter] at [2007-12-16]
Hi Vinoth,
I assume that by datagrid you are referring to datagridview. If you are referring to the older datagrid, then you will have to handle the event ItemDataBound in a similar fashion. Here is the code for the datagridview :
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If DataGridView1.Columns(e.ColumnIndex).Name = "Priority" Then
Select Case e.Value.ToString()
Case "1"
e.Value = "Critical"
Case "2"
e.Value = "Normal"
End Select
e.FormattingApplied = True
End If
End Sub
There isn't a way with the DataGrid to do this -- you'll need to use a combobox column and map your 1 and 2 to the critical and normal values.
-mark
Program Manager
Microsoft
This post is provided "as-is"
I think you can use format/parse event handler of binding to do this
For example:
in Binding.Format event, display 1- A
in Binding.Parse event,convert A - 1
It do work.
Hi vinothKumar, I hope this example helps you...
private void dgEmprestimos_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.SelectedItem ) {
//DbDataRecord is for DataReader obj. if (((DbDataRecord)e.Item.DataItem)["Status"].ToString() == "E") { e.Item.Cells[7].Text = "Emprestado"; }
else { e.Item.Cells[7].Text = "Devolvido"; }
}
}
Ricardo