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]
# 1

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

KunalYadav at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Thanks for the info.

Im using the older datagrid and i couldnt find a ItemDataBoundEvent in that .
Im using a Winforms datagrid and not a ASP.Net DataGrid.

Thanks
vinothkumar

vinxter at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
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"

MarkRideout at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
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.

JasonFang at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

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

Torns at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...