2012-09-05 158 views
0

我檢查許多網站和許多提到的代碼之前,我可以在這裏發表一個問題。我看到他們很多困惑。這是我的問題。如何從代碼GridView控件綁定後面更新相同

我有一個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消失。

請幫助我。

+0

你有什麼問題或錯誤? – SMK

回答

0

您可以在下面的RowUpdating事件中的編輯事件中找到生成的文本框,並將其分配給字符串變量。

然後有一個單獨的函數來更新輸入的值,最後打電話給你BindGrid()函數再次結合在GridView ..

注:我使用存儲過程來更新我的數據庫表。

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

    } 
0

手動你必須設置的AutoGenerateColumns = 「假」 GridView的指定列。現在你可以在你的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"; 

    } 

    } 
+0

無效的GridView1_RowDataBound(對象發件人,GridViewRowEventArgs e)只寫如果我使用模板字段...? –

0

,如果你在頁面加載調用BindData然後做到這一點:

if (!IsPostBack) 
    BindData(); 

這可能會解決你的問題......

乾杯

+0

因爲我從'Label2'綁定'GridView',直到生成'Label2'文本,所以我不能在if(!IsPostBack)中使用。它給我一個錯誤無效'Label2' –

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

} 
0

請確保您有的EnableViewState =「true」在你的GridView中設置。它解決了我的情況下的消失問題。

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 + "'"); 
} 
+3

你能解釋一下你的代碼嗎?僅考慮代碼的答案被認爲質量很低(例如,參見[this](http://meta.stackexchange.com/q/148272)) –

相關問題