开发者

Split column in asp:DataGrid

开发者 https://www.devze.com 2023-02-07 12:27 出处:网络
Should hopefully be pretty simple.I have a column in my datagrid that has emails for notification.They are separated by a \';\'.I want to put a \"<br>\" after each \';\' so that the grid doesn\'

Should hopefully be pretty simple. I have a column in my datagrid that has emails for notification. They are separated by a ';'. I want to put a "<br>" after each ';' so that the grid doesn't expand too far off of the page. I was able to get a function going that allowed me to see each row before it was created, but when I was trying to retrieve the value to edit it, it was returning null. I'm going to try and retrieve that function as I erased it on Friday, but was wondering if someone had any links or examples on how to do this.

I have:

-------------------------------------------------------------------------------
| Column1 | Column2 | Column3 |                    Notify                     |
-------------------------------------------------------------------------------
|  NULL   |  NULL   |  NULL   | email1@emailserver.com;email2@emailserver.com |
-------------------------------------------------------------------------------

I want:

---------------------------------------------------------
| Column1 | Column2 | Column3 |          Notify         |
---------------------------------------------------------
|  NULL   |  NULL   |  NULL   | email1@emailserver.com; |
|         |         |         | email2@emailserver.com  |
---------------------------------------------------------

I know that I was using the ItemCreated function and trying to retrieve the values from the DataGridItemEventArgs. Working on the function now.

Update

Ok, so I got back to this as a test:

Private Sub ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgTasks.ItemCreated
    开发者_开发知识库If e.Item.Cells.Count > 1 Then
        Dim int As Integer = 0  'I put a breakpoint here to evalute the information
    End If
End Sub

When I hit the breakpoint the first time, the .Text attribute shows that it is Notification which is exactly what it's supposed to be as the header field. On the next break, and subsequent breaks, the .Text attribute is "". Now, this grid is databound on Page_Load, but would that affect this? Wouldn't the value have to be in the DataGridItemEventArgs for that record to be created? I know when I ViewSource of the page when I'm done it shows the information there, but I don't know if that could be set after the table is drawn, but I doubt it. Any help would highly be appreciated. Thanks. Here's the Page_Load and BindData functions just incase you need them.

Page_Load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack Then
        'Do Nothing
    Else
        BindData("")
    End If
End Sub

BindData

Private Sub BindData(ByVal strSort As String)
    Dim tsDS As CustomDataSet = New CustomDataSet
    For Each dr As DataRow In tsDS.GetData("").Tables(0).Rows
        If dr.Item(2).ToString().StartsWith("Vacancy") Then
            dr.Delete()
        End If
    Next
    Dim dv As DataView = tsDS.GetData("").Tables(0).DefaultView

    If strSort.Length > 0 Then
       dv.Sort = strSort
    End If
    dgTasks.DataSource = dv
    dgTasks.DataBind()
End Sub

Here's what I get on the first Item row.

Split column in asp:DataGrid

Update: Fix

Protected Sub Item_DataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgTasks.ItemDataBound
    If e.Item.Cells.Count > 1 Then
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim notification As String = DirectCast(DirectCast(e.Item.DataItem, System.Data.DataRowView).Row, Utilities.CustomDS.Row).Notification
            e.Item.Cells(7).Text = "<a href=""mailto:" & notification & """>" & notification.Replace(";", ";<br>") & "</a>"
        End If
    End If
End Sub


I believe that you want the ItemDataBound event. ItemCreated is when that row of the DataGrid is created. The controls are there, but that’s it. On the ItemDataBound event, the controls are bound to their data, and the data is available for access. If you have logic that depends on the bound data, use the ItemDataBound event, which fires after the ItemCreated event.

You wire that up in the .aspx code by assigning the event handler name to the event, such as

OnItemDataBound = "MyDataGrid_ItemDataBound"

Within that event, you start by examining the type of item you are looking at.

Then, you find the cell and control. And finally, you set the Text property to your altered text.

Here is an example:

protected void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)

{
    if (e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
       e.Item.Cells[3].Text == <assign your altered text here>
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消