Turn editable cols on/off in single row inline edit

A

aimee

Hi. I have a datagrid with 2 editable columns, Status and
ScreenContact. When the user chooses to edit a row, one or both of
those columns may be editable depending on a separate value.
In the datagrid_EditCommand, is there a way to tell the DataGrid to
display the Status column's ItemTemplate and the ScreenContact
column's EditItemTemplate intead of both columns using their
respective EditItemTemplates?
Any help would be much appreciated.

Aimee
 
M

Mike Moore [MSFT]

Hi Aimee,

You need the ItemDataBound event. With this event we can control what goes
into each row of each column. Here is a sample based on the Pubs database.

I added a datagrid & added an edit/update/cancel column with the rest
auto-generate. Then I added the code below. It's just enough to show the
main function. When au_id starts with a 7, then one column is editable.
When it starts with an 8, then another is editable.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Bind()
End If
End Sub

Private Sub Bind()
Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT au_id, au_lname, au_fname FROM
authors"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Qry1 =
sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
DataGrid1.DataSource = Qry1
DataGrid1.DataBind()
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
If DataGrid1.EditItemIndex = e.Item.ItemIndex Then
Dim box As WebControls.TextBox
If Left(e.Item.DataItem("au_id"), 1) = 8 Then
box = New WebControls.TextBox
box.Text = e.Item.DataItem("au_lname")
e.Item.Cells(2).Controls.Add(box)
End If
If Left(e.Item.DataItem("au_id"), 1) = 7 Then
box = New WebControls.TextBox
box.Text = e.Item.DataItem("au_fname")
e.Item.Cells(3).Controls.Add(box)
End If
End If
End If
End Sub

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
Bind()
End Sub


Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 
A

aimee

Thanks Mike. I removed the EditItemTemplates from the aspx. Then in
the code-behind, I programatically added the drop-down lists in the
ItemDataBound event. However, I'm having trouble retrieving the
drop-down list when the Update Command is handled. I've tried various
ways to find the control, but each time null is returned. In the
UpdateCommand, I even iterated thru all the controls and there were no
drop-down lists anywhere.
If I add the EditItemTemplates back into the aspx, I can retrieve the
drop-down lists, but the status drop-down appears when I don't want it
to.

Do you have any insight as to what is happening to the controls I
programatically added to the table cell? and/or could you point me to
a good source of information?

Thanks in advance.


Private Sub dtGrdListing_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dtGrdListing.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
If dtGrdListing.EditItemIndex = e.Item.ItemIndex Then
'e.Item.DataItem is a ListingView object
Dim view As ListingView = CType(e.Item.DataItem,
ListingView)
If Not
view.StatusAsString.Equals(ApplicationConstants.STATUS_NEW_STR) Then
drpDwnStatus = New DropDownList
drpDwnStatus.ID = "ddStatusEdit"
drpDwnStatus.AutoPostBack = False
drpDwnStatus.DataSource = Me.StatusDataView
drpDwnStatus.DataBind()
GuiUtils.SetSelected(drpDwnStatus,
view.StatusAsString)
'7 should be the status column
e.Item.Cells(7).Controls.Clear()
e.Item.Cells(7).Controls.Add(drpDwnStatus)
End If
drpDwnContacts = New DropDownList
drpDwnContacts.ID = "drpDwnEditScreenContact"
drpDwnContacts.DataValueField = "Username"
drpDwnContacts.DataTextField = "LastFirstSbcUid"
drpDwnContacts.AutoPostBack = False
drpDwnContacts.DataSource = Me.ContactDataView
drpDwnContacts.DataBind()
GuiUtils.SetSelected(drpDwnContacts,
view.ScreenContactId)
e.Item.Cells(8).Controls.Clear()
e.Item.Cells(8).Controls.Add(drpDwnContacts)
End If
End If
End Sub

Protected Sub dtGrdListing_Update(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs) Handles dtGrdListing.UpdateCommand
Try
....
UpdateScreenContact(view, E)
....
end sub

Private Sub UpdateScreenContact(ByRef view As ListingView, ByVal E As
DataGridCommandEventArgs)
drpDwnContacts = E.Item.FindControl("drpDwnEditScreenContact")
If Me.drpDwnContacts.SelectedIndex > 0 Then
Dim contact As AAWContact =
GetContacts(drpDwnContacts.SelectedIndex)
ListingManager.Instance.ChangeScreenContact(view, contact)
End If
End Sub
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top