Turn editable cols on/off in single row inline edit

Discussion in 'ASP .Net Datagrid Control' started by aimee, Oct 28, 2003.

  1. aimee

    aimee Guest

    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
    aimee, Oct 28, 2003
    #1
    1. Advertising

  2. 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.


    --------------------
    > From: (aimee)
    > Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
    > Subject: Turn editable cols on/off in single row inline edit
    > Date: 28 Oct 2003 07:17:05 -0800
    > Organization: http://groups.google.com
    > Lines: 10
    > Message-ID: <>
    > NNTP-Posting-Host: 144.160.98.29
    > Content-Type: text/plain; charset=ISO-8859-1
    > Content-Transfer-Encoding: 8bit
    > X-Trace: posting.google.com 1067354225 8832 127.0.0.1 (28 Oct 2003

    15:17:05 GMT)
    > X-Complaints-To:
    > NNTP-Posting-Date: Tue, 28 Oct 2003 15:17:05 +0000 (UTC)
    > Path:

    cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
    e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.google.com!no
    t-for-mail
    > Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.datagridcontrol:7227
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
    >
    > 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
    >
    Mike Moore [MSFT], Oct 28, 2003
    #2
    1. Advertising

  3. aimee

    aimee Guest

    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
    aimee, Oct 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia

    Make a DataGrid row editable after user selects the row

    Luis Esteban Valencia, Jan 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    353
    Luis Esteban Valencia
    Jan 12, 2005
  2. Randall Parker
    Replies:
    1
    Views:
    602
    S. Justin Gengo
    Dec 12, 2005
  3. phl
    Replies:
    2
    Views:
    1,942
  4. robert112
    Replies:
    1
    Views:
    438
    Juan T. Llibre
    Apr 26, 2007
  5. Replies:
    1
    Views:
    133
Loading...

Share This Page