0

ここに質問を投稿する前に、多くのサイトをチェックし、多くのコードを参照しました。私はそれらを見て多くの混乱に直面しています。これが私の問題です。

私は持っていてGridView、コードビハインドから次のようにバインドしました:

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();
}

そして、同じための私のasp.netは次のようになります:

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

そして、私は私のGridView1_RowCancelingEditように書いています:

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

そして、私のようにGridView1_RowEditing見えます:

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

そして、私のようにGridView1_RowUpdating見えます:

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";
}

これから、テンプレートフィールドとデータベース列を使用してバインドしている行を取得しています。GridViewで [編集] をクリックするGridViewと、全体GridViewが消えます。

私を助けてください。

4

6 に答える 6

0

編集イベントで生成されるテキストボックスは、以下のRowUpdatingイベントにあり、文字列変数に割り当てられます。

次に、入力した値を更新する別の関数を用意し、最後にBindGrid()関数を呼び出してグリッドビューを再度バインドします。

注:ストアドプロシージャを使用してDBテーブルを更新しています。

protected void grdKeywords_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       GridViewRow row = (GridViewRow)grdKeywords.Rows[e.RowIndex];
       TextBox txtKeyword = row.FindControl("txtGridKeyword") as TextBox;
       string keyword = string.Empty;
       keyword = txtKeyword.Text;
       UpdateKeyword(keyword.ToLower());

    }



public int UpdateKeyword(string strKeyword)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());
        SqlCommand cmdUpdateKeyword = BuildCommand(conn, "proc_UpdateKeyword");
        cmdUpdateKeyword.Parameters.AddWithValue("@Keyword", strKeyword);
        conn.Open();
        int i = Convert.ToInt32(cmdUpdateKeyword.ExecuteScalar());
        conn.Close();
        BindGrid();

    }
于 2012-09-05T07:20:46.260 に答える
0

これを試して ..

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    GridView1.EditIndex = e.RowIndex;
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];


    TextBox PrStyle = (TextBox)row.FindControl("PrStyle");
    TextBox PrID = (TextBox)row.FindControl("PrID");

    GridView1.EditIndex = -1;


    SqlCommand cmd = new SqlCommand("Update dt Set PrStyle='" + PrStyle + "',PrID='" + PrID + "'");
}
于 2015-05-15T10:25:38.827 に答える
0

グリッドビューの列を手動で指定するには、 AutoGenerateColumns="false"を設定する必要があります。これで、GridView で必要な列を指定できるようになりました。「BoundFields」と「TemplateFields」の両方を使用できるようになりました。以下に示すように。

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
        <Columns>
            <asp:BoundField DataField="column1" HeaderText="Column1" SortExpression="" />
          <asp:BoundField DataField="column2" HeaderText="Column2" SortExpression="" />
            <asp:TemplateField SortExpression="points">
            <HeaderTemplate>HELLO</HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("hello") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

これで、次を使用できます。

GridView.RowDataBoundデータをデータ行にバインドするイベント。

 void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {

      Label Label1= ((Label)e.Row.FindControl("Label1"));
      Label1.Text = "YOURDATA";

    }

  }
于 2012-09-05T07:36:31.453 に答える
0

ページの読み込みで BindData を呼び出す場合は、次のようにします。

if (!IsPostBack)
    BindData();

それはあなたの問題を解決するかもしれません...

乾杯

于 2012-09-05T08:02:45.823 に答える
0

GridView に EnableViewState="true" が設定されていることを確認してください。私の場合、それは消失の問題を解決しました。

于 2014-04-24T10:38:06.893 に答える