2010-07-06 5 views
-1

comment mettre en œuvre jqGrid dans asp.netjqGrid en asp.net

+0

Vous devriez écrire plus d'information ce que vous cherchez. Avez-vous l'intention d'utiliser WFC, Web Forms, ASP.NET MVC ou un mélange de là-bas? Vous pouvez tout écrire en langage HTML pur avec javascripts et utiliser WFC pour fournir des données. Alors ce ne sera pas vraiment ASP.NET, mais ça marchera bien. Où allez-vous enregistrer vos données? Sur Microsoft SQL Server ou par exemple dans des fichiers XML? Voulez-vous utiliser JSON ou XML? Que prévoyez-vous (probablement devez-vous) utiliser en tant que couche de données: Entity Framework, LINQ-to-SQL, SQLDataReader et ainsi de suite? Pouvez-vous utiliser .NET 4.0? Je peux continuer. Il y a trop de chemins qui vont dans des directions différentes ... – Oleg

Répondre

0

Vous pouvez commencer à partir link1 et link2.

Un peu plus instructions

+0

Ces liens sont pour le produit Trirand. Pas libre, contrairement au projet open source jqgrid. –

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

Ensuite, vous devez obtenir vos données dans la grille et définissez vos événements.

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

Ensuite, vous avez besoin de vos données

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; 

} 

Passez ensuite à la grille

OnLoad

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

Enfin ce que vous faites avec les données .

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

ne hésitez pas à signaler les erreurs: P: PI a écrit à partir du haut de ma tête, p il n'a pas été testé

Theres aussi php façon, jquery et chemin mvc, mais l'homme qui est trop beaucoup à écrire: P donc ici c'est la manière de base.