Modifying a cell on the datagrid control - Please help !!!

B

bienwell

Hi all,

I have a datagrid control declared as below:

<asp:DataGrid id="DGrid_Carrier" runat="server"
OnItemDataBound="DGrid_ItemDataBound" Width="353px" Font-Size="Smaller"
Font-Names="Arial" CellSpacing="1" CellPadding="3" EnableViewState="False"
AutoGenerateColumns="False">
<AlternatingItemStyle backcolor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle backcolor="LightYellow"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Account_ID"
HeaderText="Account_ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Carrier_Name" HeaderText="Carrier
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Carrier_Entity" HeaderText="Carrier
Entity"></asp:BoundColumn>
<asp:BoundColumn DataField="First_Name" HeaderText="First
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_Address"
HeaderText="Office_Address"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_City"
HeaderText="Office_City"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_ZipCode"
HeaderText="Office_ZipCode"></asp:BoundColumn>
<asp:BoundColumn DataField="Work_Phone"
HeaderText="Work_Phone"></asp:BoundColumn>
<asp:BoundColumn DataField="Start_Date"
HeaderText="Start_Date"></asp:BoundColumn>
<asp:BoundColumn DataField="End_Date"
HeaderText="End_Date"></asp:BoundColumn>
<asp:BoundColumn DataField="End_Date"
HeaderText="End_Date"></asp:BoundColumn>
<asp:placeHolder id="PlaceHolder1" runat="server" />
</Columns>
</asp:DataGrid>

The requirement is to check the filed "End_Date" from the table. If this
field is empty then I need to display a check box on that cell of the
datagrid control.
When user check on the check box in the cell, I need to update Current
date to the field "End Date" of that table.

I was working with displaying the checkbox(es) on the cells and tried to
replace an empty field on the cell of datagrid control by a check box . I'd
like to declare a placeholder in the datagrid control and get the error :
"System.Web.UI.WebControls.DataGridColumnCollection must have items of type
'System.Web.UI.WebControls.DataGridColumn'. 'asp:placeHolder' is of type
'System.Web.UI.WebControls.PlaceHolder'."

Do you know where I can declare the placeholder ? ?? I worked with
DataList control . Besides, what is the event hander I should use when user
click on the check box to update the Cureent date to End_Date field ???
I'm thinking about "SelectedIndexChange". Is that correct ??

Please help me !!! Thank you.


Sub DGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim EDate As Object = CType(drv.row("End_Date"),Object)
If IsDbNull(EDate) Then
Dim myCheckBox As Checkbox = New CheckBox()
myCheckBox.Text = "Check this box to set today as the
end date."
PlaceHolder1.Controls.Add(myCheckBox)
End If

End If

End Sub
 
E

Elton Wang

Hi Bienwell,

You can directly add a checkbox to the datagrid and set
its visibility in ItemDataBound event according to
End_Date. But the problem is event handler.
SelectedIndexChanged doesn't work. Although you can set
checkbox's autopostback to true, you need catch the event
and handle it yourself, because it's one control inside
the datagrid. Actually you can think to use ButtonColumn,
it's easy to handle event in ItemCommand.

Elton Wang
(e-mail address removed)
-----Original Message-----
Hi all,

I have a datagrid control declared as below:

<asp:DataGrid id="DGrid_Carrier" runat="server"
OnItemDataBound="DGrid_ItemDataBound" Width="353px" Font-Size="Smaller"
Font-Names="Arial" CellSpacing="1" CellPadding="3" EnableViewState="False"
AutoGenerateColumns="False">
<AlternatingItemStyle
backcolor="#E0E0E0"> said:
<HeaderStyle backcolor="LightYellow"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Account_ID"
HeaderText="Account_ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Carrier_Name" HeaderText="Carrier
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Carrier_Entity" HeaderText="Carrier
Entity"></asp:BoundColumn>
<asp:BoundColumn DataField="First_Name" HeaderText="First
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_Address"
HeaderText="Office_Address"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_City"
HeaderText="Office_City"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_ZipCode"
HeaderText="Office_ZipCode"></asp:BoundColumn>
<asp:BoundColumn DataField="Work_Phone"
HeaderText="Work_Phone"></asp:BoundColumn>
<asp:BoundColumn DataField="Start_Date"
HeaderText="Start_Date"></asp:BoundColumn>
<asp:BoundColumn DataField="End_Date"
HeaderText="End_Date"></asp:BoundColumn>
<asp:BoundColumn DataField="End_Date"
HeaderText="End_Date"></asp:BoundColumn>
<asp:placeHolder id="PlaceHolder1" runat="server" />
</Columns>
</asp:DataGrid>

The requirement is to check the filed "End_Date" from the table. If this
field is empty then I need to display a check box on that cell of the
datagrid control.
When user check on the check box in the cell, I need to update Current
date to the field "End Date" of that table.

I was working with displaying the checkbox(es) on the cells and tried to
replace an empty field on the cell of datagrid control by a check box . I'd
like to declare a placeholder in the datagrid control and get the error :
"System.Web.UI.WebControls.DataGridColumnCollection must have items of type
'System.Web.UI.WebControls.DataGridColumn'. 'asp:placeHold er' is of type
'System.Web.UI.WebControls.PlaceHolder'."

Do you know where I can declare the placeholder ? ?? I worked with
DataList control . Besides, what is the event hander I should use when user
click on the check box to update the Cureent date to End_Date field ???
I'm thinking about "SelectedIndexChange". Is that correct ??

Please help me !!! Thank you.


Sub DGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType =
ListItemType.AlternatingItem Then
 
B

bienwell

Elton,

If I add ButtonColumn on the cell of the datagrid control, do I need to
declare a placeholder for that ? I tried to declare a placeholder in the
datagrid control before and got an error. Do you have a sample code to add
Buttoncontrol on the cell(s) ?

Thanks


================
 
E

Elton Wang

Hi Bienwell,

No need for placeholder. Following shows how to implement
it:

--Add ButtonColumn to the datagrid

<Columns>
<asp:ButtonColumn ButtonType="LinkButton" Text="Update End
Date" HeaderText="Update"
CommandName="Update"></asp:ButtonColumn>
.... other columns
</Columns>


--In ItemDataBound Event
Dim drv As DataRowView = CType(e.Item.DataItem,
DataRowView)
Dim EDate As Object = CType(drv.Row("End_Date"), Object)
' suppose the Update Button is in the first column
Dim cell As TableCell = e.Item.Cells(0)
If Not IsDBNull(EDate) Then
cell.Visible = False ' if exist end_date set button
invisible
Else
cell.ToolTip = "Check here to set today as the end
date."
End If

--In ItemCommand event

Dim commandName As String = e.CommandName
If commandName.Equals("Update") Then
' Update db
End if

--------

HTH


Elton Wang
 
B

bienwell

Elton,

I've got you. Thanks a lot.



Elton Wang said:
Hi Bienwell,

No need for placeholder. Following shows how to implement
it:

--Add ButtonColumn to the datagrid

<Columns>
<asp:ButtonColumn ButtonType="LinkButton" Text="Update End
Date" HeaderText="Update"
CommandName="Update"></asp:ButtonColumn>
... other columns
</Columns>


--In ItemDataBound Event
Dim drv As DataRowView = CType(e.Item.DataItem,
DataRowView)
Dim EDate As Object = CType(drv.Row("End_Date"), Object)
' suppose the Update Button is in the first column
Dim cell As TableCell = e.Item.Cells(0)
If Not IsDBNull(EDate) Then
cell.Visible = False ' if exist end_date set button
invisible
Else
cell.ToolTip = "Check here to set today as the end
date."
End If

--In ItemCommand event

Dim commandName As String = e.CommandName
If commandName.Equals("Update") Then
' Update db
End if

--------

HTH


Elton Wang
 

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