Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi guys, ive got a problem here. ive created a gridvew where i want updating to be true. my problem is when i hit the edit button the gridview hides(i think) unless i search in again through my drop down list (ive created a dropdownlist because i only want specific data to be viewed) and when i return to my previous grid state now the it is ready to be updated
is there any missing codes?
plss help
here is my code:
On source
<pre lang="xml"><asp:GridView ID="TPdataCCXXXXX" runat="server" Height="439px" Width="854px" AutoGenerateColumns="False"
        onrowcancelingedit="TPdataCCXXXXX_RowCancelingEdit"
        onrowediting="TPdataCCXXXXX_RowEditing" onrowupdating="TPdataCCXXXXX_RowUpdating">
            <Columns>
               <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" />

       <asp:TemplateField HeaderText="CourseTitle">
          <ItemTemplate>
            <%#Eval("CourseTitle")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtCourseTitle" Text='<%# Eval("CourseTitle")%>' />
          </EditItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderText="CourseCode">
          <ItemTemplate>
            <%#Eval("CourseCode")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtCourseCode" Text='<%# Eval("CourseCode")%>' />
          </EditItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderText="Objectives">
          <ItemTemplate>
            <%#Eval("Objectives")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtObjectives" Text='<%# Eval("Objectives")%>' />
          </EditItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderText="Duration">
          <ItemTemplate>
            <%#Eval("Duration")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtDuration" Text='<%# Eval("Duration")%>' />
          </EditItemTemplate>
      </asp:TemplateField>


            </Columns>
        </asp:GridView>


on my VB codes
<pre lang="vb">Imports System.Web
Imports System
Imports System.Data
Partial Class updateTP
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If drpcc.Text = "CCXXXXX" Then
            Dim strSQL As String
            Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
            strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan] WHERE ([CostCenter] = @CostCenter)"
            connection.Open()
            Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
            myCommand.Parameters.AddWithValue("@CostCenter", drpcc.Text)
            TPdataCCXXXXX.DataSource = myCommand.ExecuteReader()
            TPdataCCXXXXX.DataBind()
        ElseIf drpcc.Text = "123" Then
            Dim strSQL As String
            Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
            strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan] WHERE ([CostCenter] = @CostCenter)"
            connection.Open()
            Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
            myCommand.Parameters.AddWithValue("@CostCenter", drpcc.Text)
            TPdataCCXXXXX.DataSource = myCommand.ExecuteReader()
            TPdataCCXXXXX.DataBind()
        End If
    End Sub
    Protected Sub TPdataCCXXXXX_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles TPdataCCXXXXX.RowUpdating
        Dim row As GridViewRow = TPdataCCXXXXX.Rows(e.RowIndex)
        Dim txtCourseTitle As TextBox = row.FindControl("txtCourseTitle")
        Dim txtCourseCode As TextBox = row.FindControl("txtCourseCode")
        Dim txtObjectives As TextBox = row.FindControl("txtObjectives")
        Dim txtDuration As TextBox = row.FindControl("txtDuration")
    End Sub

    Protected Sub TPdataCCXXXXX_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles TPdataCCXXXXX.RowEditing
        TPdataCCXXXXX.EditIndex = e.NewEditIndex
        TPdataCCXXXXX.DataBind()
    End Sub

    Protected Sub TPdataCCXXXXX_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles TPdataCCXXXXX.RowCancelingEdit
        TPdataCCXXXXX.EditIndex = -1
        TPdataCCXXXXX.DataBind()
    End Sub

Thanks in advance and more power
Posted

It is looking that you are binding the grid based on the selection of the "drpcc", So I think the problem is with the binding of the drpcc. Because the selection might changed of the drpcc and it is not going into any of the condition e.g. drpcc.Text = "CCXXXXX" and other.
 
Share this answer
 
Comments
janwel 10-May-11 2:08am    
sir so what do you think is the solution?
nit_singh 10-May-11 2:44am    
Check the selection should not change of the dropdown, the problem might be that you are againg binding the DropDown
You don't need any if..else block in your code block i.e.
VB
If drpcc.Text = "CCXXXXX" Then...


because the IF and ELSE IF are identical and the
VB
myCommand.Parameters.AddWithValue("@CostCenter", drpcc.Text)

anyway taking drpcc.Text i.e. current value of your drop down.

thanks,
Hemant
 
Share this answer
 
Thanks Sir nit_singh
Ive found the error, ive created another sub named BindGrid and thats what im calling on my editing events
<pre lang="vb">Protected Sub TPdataCCXXXXX_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles TPdataCCXXXXX.RowEditing
    TPdataCCXXXXX.EditIndex = e.NewEditIndex
    BindGrid()
End Sub

Protected Sub TPdataCCXXXXX_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles TPdataCCXXXXX.RowCancelingEdit
    TPdataCCXXXXX.EditIndex = -1
    BindGrid()
End Sub
Public Sub BindGrid()
    If drpcc.Text = "CCXXXXX" Then
        Dim strSQL As String
        Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan] WHERE ([CostCenter] = @CostCenter)"
        connection.Open()
        Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
        myCommand.Parameters.AddWithValue("@CostCenter", drpcc.Text)
        TPdataCCXXXXX.DataSource = myCommand.ExecuteReader()
        TPdataCCXXXXX.DataBind()
    ElseIf drpcc.Text = "123" Then
        Dim strSQL As String
        Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan] WHERE ([CostCenter] = @CostCenter)"
        connection.Open()
        Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
        myCommand.Parameters.AddWithValue("@CostCenter", drpcc.Text)
        TPdataCCXXXXX.DataSource = myCommand.ExecuteReader()
        TPdataCCXXXXX.DataBind()
    End If
End Sub
 
Share this answer
 
public void bind()
{
SqlDataAdapter da = new SqlDataAdapter(&quot;select empno from emp&quot;, cn);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = &quot;empno&quot;;
DropDownList1.DataValueField = &quot;empno&quot;;
DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cn;
cmd.CommandText = &quot;getemp&quot;;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue(&quot;@empno&quot;, DropDownList1.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900