• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Specify parameter value updating gridview

I have a gridview that is bound to a sqldatasource. When the user enters a complete date, I want to programmatically set the priority parameter value to "PEND"
I don't think this is rocket science but the "how" of it all escapes me.
I'm attaching both the aspx code and the code behind and would really appreciate some help.
<asp:GridView ID="tdGridView" runat="server" AutoGenerateColumns="False" BackColor="White"
        BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="rec_id"
        DataSourceID="NewTDSqlDataSource" GridLines="Horizontal">
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <Columns>
          <asp:CommandField ShowEditButton="True" />
          <asp:TemplateField HeaderText="rec_id" InsertVisible="False" SortExpression="rec_id">
            <EditItemTemplate>
              <asp:Label ID="Label1" runat="server" Text='<%# Eval("rec_id") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
              <asp:Label ID="Label1" runat="server" Text='<%# Bind("rec_id") %>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="priority" SortExpression="priority">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("priority") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
              <asp:Label ID="Label2" runat="server" Text='<%# Bind("priority") %>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="tool_design_complete_date" SortExpression="tool_design_complete_date">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("tool_design_complete_date") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
              <asp:Label ID="Label3" runat="server" Text='<%# Bind("tool_design_complete_date") %>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="design_notes" SortExpression="design_notes">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("design_notes") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
              <asp:Label ID="Label4" runat="server" Text='<%# Bind("design_notes") %>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
      </asp:GridView>
<asp:SqlDataSource ID="NewTDSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:App_DBConnectionString %>"
        SelectCommand="SELECT [rec_id], [priority], [tool_design_complete_date], [design_notes] FROM [Toolroom] WHERE (([complete_date] IS NULL) AND ([tool_design] = @tool_design) AND ([priority] <> @priority))" 
        UpdateCommand="UPDATE [Toolroom] SET [priority] = @priority, [tool_design_complete_date] = @tool_design_complete_date, [design_notes] = @design_notes WHERE [rec_id] = @rec_id">
        <SelectParameters>
          <asp:Parameter DefaultValue="YES" Name="tool_design" Type="String" />
          <asp:Parameter DefaultValue="PEND" Name="priority" Type="String" />
        </SelectParameters>
        <UpdateParameters>
          <asp:Parameter Name="priority" Type="String" />
          <asp:Parameter DbType="DateTime" Name="tool_design_complete_date" />
          <asp:Parameter Name="design_notes" Type="String" />
          <asp:Parameter Name="rec_id" Type="Decimal" />
        </UpdateParameters>
      </asp:SqlDataSource>
*CODE BEHIND*
Protected Sub tdGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles tdGridView.RowUpdating
    If IsDate(e.NewValues("tool_complete_date")) Then
      Me.ToolDesignSqlDataSource.UpdateCommand = ""
      e.NewValues("priority") = "PEND"    
    End If
  End Sub

Open in new window

0
lakhi
Asked:
lakhi
  • 2
1 Solution
 
robastaCommented:
TYPO:

 If IsDate(e.NewValues("tool_complete_date")) Then

should be

 If IsDate(e.NewValues("tool_design_complete_date")) Then
0
 
lakhiAuthor Commented:
The problem turned out to be "NewValues" is not accessible when templates are used in the gridview. It's necessary to use "FindControl"
I went back to my original and tried this but I'm still missing somethin because the complete date gets updated but the priority remains unchanged:
Protected Sub toolDesignGridview_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles tdcGridview.RowUpdating
    ' Dim gvr As GridViewRow = Me.toolDesignGridview.Rows(Me.toolDesignGridview.EditIndex)
    Dim tb1 As TextBox = CType(tdcGridview.Rows(e.RowIndex).FindControl("toolDesignCompDateTextBox"), TextBox)
    Dim priTB As TextBox = CType(tdcGridview.Rows(e.RowIndex).FindControl("priTextBox"), TextBox)

    If IsDate(tb1.Text) Then priTB.Text = "PEND"
  End Sub

Open in new window

0
 
lakhiAuthor Commented:
This is what I wound up doing and it finally worked:
Protected Sub toolDesignGridview_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles tdcGridview.RowUpdating
    Dim tb1 As TextBox = CType(tdcGridview.Rows(e.RowIndex).FindControl("toolDesignCompDateTextBox"), TextBox)
    Dim priTB As TextBox = CType(tdcGridview.Rows(e.RowIndex).FindControl("priTextBox"), TextBox)

    If IsDate(tb1.Text) Then
      Me.ToolDesignSqlDataSource.UpdateParameters("priority").DefaultValue = "PEND"
    Else
      Me.ToolDesignSqlDataSource.UpdateParameters("priority").DefaultValue = priTB.Text
    End If
  End Sub

And I adding the "RowUpdating" to the gridview
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now