2009-04-18 14 views
0

編集可能なグリッドに、MSSQL Server'05の異なるテーブルのデータを関数にコードを書き込む方法を教えてください。GridView in ASP.NET 2.0

私が使用している:

Dim conn As New SqlConnection(conn_web) 
    Dim objCmd As New SqlDataAdapter(sql, conn) 
    Dim oDS As New DataSet 

    objCmd.Fill(oDS, "TAB") 

    Dim dt As DataTable = oDS.Tables(0) 
    Dim rowCount As Integer = dt.Rows.Count 
    Dim dr As DataRow = dt.NewRow() 
    If rowCount = 0 Then 
     e.DataSource = Nothing 
     e.DataBind() 
     e.Focus() 

    Else 
     e.DataSource = dt 
     e.DataBind() 
    End If 
+2

私は質問を理解していません。 –

答えて

0

を私はこれがe変数に沸くと思う:それは何を参照していますか?

Page_LoadメソッドのEventArgs引数が怖いです(私は正しいのでしょうか?)。

あなたがしようとしていることを行うには、GridView Webコントロールのインスタンスを参照する必要があります。

0

あなたの質問は私にはあまり明確ではないが、多分これは役立ちます:

複数のテーブルからデータを取得し、あなたが得る結果と、あなたのデータセットを埋めるために、ストアドプロシージャを記述します。これで、データセットには、ストアドプロシージャから返されたすべてのフィールドを含むテーブルが作成されます。これは、明らかにSQLクエリに基づいて異なるテーブルから取得されます。

戻ったデータセットでグリッドをバインドします。

0

GridViewにバインドする前にDataRowを操作することは可能です。これにより、SQLクエリー自体を変更せずに、プログラムでデータに変更を加えることができます。

まず、DataRowのコピーを作成します。次に、行を追加したり、行の列の値を変更したりします。次に、DataRowでEndEdit()とAcceptChanges()を呼び出して、変更が確実に適用されるようにします。次に、変更した行をImportRowを使用してDataSetのテーブルに追加し、RowStateをAddedに変更し、最後にDataRowを使用してTableAdapterのUpdateメソッドを呼び出します。ここで

は私のノートからいくつかのサンプルコードです:

try 
{ 
    DataSet objDataSet = new DataSet(); 
    SqlConnection objSqlConnection = new SqlConnection(); 
    objSqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); 
    string strSQL = "SELECT * FROM table WHERE OrderDate = '2008.10.21' AND OrderSequence = 1;"; 
    SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(strSQL, objSqlConnection); 
    objSqlDataAdapter.Fill(objDataSet); 

    // Create a copy of the DataRow 
    DataRow drOrders = objDataSet.Tables[0].Rows[0]; 
    // Change the value of some columns to avoid duplicate primary key 
    drOrders["OrderDate"] = DateTime.Now.ToString("yyyy.MM.dd"); 
    drOrders["OrderSequence"] = 15; 
    // Call EndEdit() and AcceptChanges() on the DataRow to ensure changes are applied 
    drOrders.EndEdit(); 
    drOrders.AcceptChanges(); 
    // Add the modified row back into the DataSet's table using ImportRow 
    objDataSet.Tables[0].ImportRow(drOrders); 
    // Change the RowState to Added 
    objDataSet.Tables[0].Rows[1].SetAdded(); 
    // Call a TableAdapter's Update method using the DataRow 
    dsOrderTableAdapters.DetailFareTableAdapter dfta = new dsOrderTableAdapters.DetailFareTableAdapter(); 
    dfta.Update(objDataSet.Tables[0].Rows[1]); 

    lblMessage.Text = "The row has been copied!"; 
} 
catch (Exception ex) 
{ 
    lblErrorMessage.Text = ex.ToString(); 
}