2010-05-13 63 views
0

我已經寫了下面的內容來顯示datagrid中的數據,並且插入新行,但是如何執行更新和刪除plss幫助這裏是我的代碼 using System; using System.Data; using System.Configuration;使用System.Collections的 ; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;使用Telerik.Web.UI的 ; using System.Data.SqlClient;radgridview在asp.net中刪除更新

public partial class Default6:System.Web.UI.Page string strQry,strCon;

SqlDataAdapter da; 
SqlConnection con; 
DataSet ds; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    strCon = "Data Source=MINETDEVDATA; Initial Catalog=ML_SuppliersProd; User Id=sa; [email protected];"; 
    con = new SqlConnection(strCon); 
    strQry = "SELECT * FROM table1"; 
    da = new SqlDataAdapter(strQry, con); 
    SqlCommandBuilder cmdbuild = new SqlCommandBuilder(da); 
    ds = new DataSet(); 
    da.Fill(ds, "table1"); 
    RadGrid1.DataSource = ds.Tables["table1"]; 
    RadGrid1.DataBind(); 
    Label3.Visible = false; 
    Label4.Visible = false; 
    Label5.Visible = false; 
    txtFname.Visible = false; 
    txtLname.Visible = false; 
    txtDesignation.Visible = false; 

} 
protected void Submit_Click(object sender, EventArgs e) 
{ 
    Label3.Visible = true; 
    Label4.Visible = true; 
    Label5.Visible = true; 
    txtFname.Visible = true; 
    txtLname.Visible = true; 
    txtDesignation.Visible = true; 
} 
protected void Button2_Click(object sender, EventArgs e) 
{ 
    DataSet ds = new DataSet("EmployeeSet"); 
    da.Fill(ds, "table1"); 



    DataTable EmployeeTable = ds.Tables["table1"]; 
    DataRow row = EmployeeTable.NewRow(); 
    row["Fname"] = txtFname.Text.ToString(); 
    row["Lname"] = txtLname.Text.ToString(); 
    row["Designation"] = txtDesignation.Text.ToString(); 
    EmployeeTable.Rows.Add(row); 
    da.Update(ds, "table1"); 
    //RadGrid1.DataSource = ds.Tables["table1"]; 
    //RadGrid1.DataBind(); 
    txtFname.Text = ""; 
    txtLname.Text = ""; 
    txtDesignation.Text = ""; 


} 






protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e) 
{ 


    } 
} 

}

回答

0

您可以使用這個對象數據源的選擇刪除和​​更新方法

你可以拖放對象的數據源&然後進行配置。

&將objectDatasource賦值給Grid。

&你可以把一個不同勢層像業務邏輯層方法.. &那麼你可以給那些方法對象數據源

&可以連接的ObjectDataSource &電網通過主鍵的表。

0

當您分配EmployeeTable類型的新行時,您可以將任何選定行分配給數據行,然後進行更改。

DataTable EmployeeTable = ds.Tables["table1"]; 
DataRow row = EmployeeTable.rows[Radgrid.selectedIndex]; 
row["Fname"] = txtFname.Text.ToString(); 
row["Lname"] = txtLname.Text.ToString(); 
row["Designation"] = txtDesignation.Text.ToString(); 
EmployeeTable.Rows.Add(row); 
da.Update(ds, "table1"); 

試試這個。我沒有測試過,但這應該起作用。