So binden Sie ein GridView-Steuerelement von code-behind und Aktualisierung der

Überprüfe ich viele Websites und bezeichnet viele codes, bevor ich posten könnte einer Fragen hier. Ich bin mit viel Verwirrungen, Sie zu sehen. Hier ist mein problem.

Ich habe eine GridView und ich gebunden haben es aus dem code hinter als :

public void BindData()
{
    SqlCommand comd = new SqlCommand("SELECT * FROM " + Label2.Text + "", con);
    SqlDataAdapter da = new SqlDataAdapter(comd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    GridView2.DataSource = dt;
    GridView2.DataBind();
}

Und meine asp.net für das gleiche sieht wie folgt aus :

<asp:GridView ID="GridView1" runat="server" ForeColor="#333333" 
                AutoGenerateEditButton="True" DataKeyNames="Locations" 
                onrowcancelingedit="GridView1_RowCancelingEdit" 
                onrowdatabound="GridView1_RowDataBound" 
                onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

    <Columns>
        <asp:TemplateField HeaderText="Locations">
            <ItemTemplate>
                <asp:Label ID="LblLocations" runat="server" Text='<%#Eval("Locations") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Lamp_Profile1">
            <ItemTemplate>
                <asp:Label ID="LblLamp_Profile1" runat="server" Text='<%#Eval("Lamp_Profile1") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="LblEditLamp_Profile1" runat="server" Text='<%#Eval("Lamp_Profile1") %>'></asp:Label>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Fan_Profile1">
            <ItemTemplate>
                <asp:Label ID="LblFan_Profile1" runat="server" Text='<%#Eval("Fan_Profile1") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="LblEditFan_Profile1" runat="server" Text='<%#Eval("Fan_Profile1") %>'></asp:Label>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="AC_Profile1">
            <ItemTemplate>
                <asp:Label ID="LblAC_Profile1" runat="server" Text='<%#Eval("AC_Profile1") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="LblEditAC_Profile1" runat="server" Text='<%#Eval("AC_Profile1") %>'></asp:Label>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>

    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

Und ich habe geschrieben das mein GridView1_RowCancelingEdit wie:

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    e.Cancel = true;
    GridView1.EditIndex = -1;
}

Und meine GridView1_RowEditing aussieht:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindData();
}

Und meine GridView1_RowUpdating aussieht:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridView1.EditIndex = e.RowIndex;

    Label Locations = GridView1.Rows[e.RowIndex].FindControl("LblLocations") as Label;
    //ViewState["Locations_Instance"] = Locations.Text;

    Label Lamp_Profile1 = GridView1.Rows[e.RowIndex].FindControl("LblLamp_Profile1") as Label;
    Label Fan_Profile1 = GridView1.Rows[e.RowIndex].FindControl("LblFan_Profile1") as Label;
    Label AC_Profile1 = GridView1.Rows[e.RowIndex].FindControl("LblAC_Profile1") as Label;

    string query = "UPDATE " + Label3.Text + " SET Lamp_Profile1 ='" + Lamp_Profile1 + "', Fan_Profile1 ='" + Fan_Profile1 + "', AC_Profile1 ='" + AC_Profile1 + "' WHERE Locations = '" + Locations + "'";
    com = new SqlCommand(query, con);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
    GridView1.EditIndex = -1;

    BindData();
    //lbldisplay.Text = "Updated Successfully";
}

Von mir bekommen, was je Zeile bin ich verbindlich mit der Vorlage Feld als auch Datenbank-Spalten in meinem GridView und Sobald ich auf Edit klicke in der GridView die ganze GridView verschwindet.

Bitte helfen Sie mir.

  • Was für ein problem oder Fehler haben Sie?
InformationsquelleAutor Archana B.R | 2012-09-05
Schreibe einen Kommentar