//your grid tag
<trirand:JQGrid runat="server" ID="JQGrid1" OnRowEditing="JQGrid1_RowEditing" Width="650">
<Columns>
//your column tag
<trirand:JQGridColumn
DataField="CustomerID"
Editable="true"
EditType="DropDown"
EditorControlID="CustomersDdl"
Width="120" />
</Columns>
//your additional stuff eg.toolbars events bla bla
<ClientSideEvents RowSelect="RowSelect"/>
</trirand:JQGrid>
然后您需要将数据放入网格并设置您的事件。
你的js
var lastSelection;
function RowSelect(id) {
if (id && id !== lastSelection) {
var grid = jQuery("#<%= JQGrid1.ClientID %>");//if you use .js file
//you need to put this on the page and return its value in a method
grid.restoreRow(lastSelection);
grid.editRow(id, true);
lastSelection = id;
}
}
那么你需要你的数据
public DataTable GetData()
{
//may wanna use tryCatch
//Connect to database
MySqlConnection connect = Database.Connect()//Connect to whatever
string sql = string.Format("your SQL Statment");
//Best to use DataTable to load the grid
MySqlDataAdapter adapter = new MySqlDataAdapter(sql,connection);
DataTable dt = new DataTable();
adapter.Fill(dt);
adapter.Dispose();
Database.Disconnect(connection);
return dt;
}
然后将其传递给网格
加载
protected void Page_Load(object sender, EventArgs e)
{
if (JQGrid1.AjaxCallBackMode == AjaxCallBackMode.RequestData) // or was it RowEdit hm?
{
DataTable dt = GetData();
JQGrid1.DataSource = dt;
JQGrid1.DataBind();
//You may want to store data in Session var as well;
Session["data"] = dt; //To retriev just cast back like this //(DataTable)Session["data"]
}
else
{
//put your other code here if you dont want to do stuff on async callback
//like loading whole fricken page all over again each time you save row
}
}
最后你如何处理数据。
protected void JQGrid1_RowEditing(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
{
//either get grid or get sender
JQGrid grid = (JQGrid)sender;
DataTable dt = (DataTable)Session["data"] //or use (DataTable)grid.DataSource
dt.PrimaryKey = new DataColumn[] { dt.Columns["DatabaseColName"] };
DataRow rowEdited = dt.Rows.Find(e.RowKey);
//rowEdited[""] is the row which you have now
//e.RowData[""] is the changed row use database column names if using datatable
//else use whatever
rowEdited["CustomerID"] = e.RowData["CustomerID"];
rowEdited["Freight"] = e.RowData["Freight"];
JQGrid1.DataSource = dt;
JQGrid1.DataBind();
//Finaly save to db
Record rec = new Record(rowEdited["CustomerID"].ToString(),rowEdited["Freight"].ToString());
rec.SaveToDb();
}
请随意指出错误:P:P我是从头顶写的;p所以它没有经过测试
还有php方式,jquery和mvc方式,但是写的太多了:P所以这里是基本方式。