2012-03-30 21 views
0
con.Open(); 
string str = DropDownList1.SelectedValue; 
SqlDataAdapter da = new SqlDataAdapter(@" 
    select distinct 
      a.DepotCode, a.CustWt, b.CustName, 
      b.Lat_Degree, b.Lat_Minute, b.Lat_Second, 
      b.Lon_Degree, b.Lon_Minute, b.Lon_Second 
    from tblDepotCustMapping a, tblCustomers b 
    where DepotCode='" + str + "' and a.CustCode=b.CustCode", con); 
DataSet ds = new DataSet(); 
da.Fill(ds); 
GridView1.DataSource = ds; 
GridView1.DataBind(); 

con.Close(); 

我想使用此gridview值來計算..如何訪問網格中的值..幫助我..我有一個gridview自動生成的查詢..我需要訪問gridview中的值在下一個gridview進一步計算

回答

0

通過底層數據集而不是通過GridView訪問值將會更容易。 IE:

con.Open(); 
string str = DropDownList1.SelectedValue; 
SqlDataAdapter da = new SqlDataAdapter("select distinct a.DepotCode,a.CustWt, b.CustName,b.Lat_Degree,b.Lat_Minute,b.Lat_Second,b.Lon_Degree,b.Lon_Minute,b.Lon_Second from tblDepotCustMapping a, tblCustomers b where DepotCode='" + str + "'and a.CustCode=b.CustCode", con); 
DataSet ds = new DataSet(); 
da.Fill(ds); 
GridView1.DataSource = ds; 
GridView1.DataBind(); 
con.Close(); 

您可以使用DataSet變量ds訪問實際數據。

然後是這樣的:

DataTable t = ds.Tables[0]; 

// Manipulate and calculate your data here. IE: 
foreach(DataRow row in t.Rows) 
{ 
    row["column_name"] = 'some calculated value or your manipulated value here'; 
} 
0

有在您的方案很多方法,但結合GridView1後的答案很簡單,就在您的數據集中的數據表(DS)循環,然後結合該表到其他的GridView

0

在綁定到GridView之前,使用DataSet中的數據進行計算會更容易。

但是,如果要訪問網格中的項目,可以使用gridview RowDataBound事件在服務器控件綁定到數據源之後執行某些操作。

如果您有這方面的GridView在你的頁面:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" 
       OnRowDataBound="GridView1_RowDataBound" /> 

你可以把事件處理程序在你的代碼隱藏文件。將一個GridViewRowEventArgs對象傳遞給事件處理方法,該方法允許您訪問引發該事件的行的屬性。

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
    // Access and modify the cells of your GridView. 
    e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>"; 

    // Retrieve the underlying data item. 
    DataRowView rowView = (DataRowView)e.Row.DataItem; 

    // Retrieve the DepotCode value for the current row. 
    String depot = rowView["DepotCode"].ToString(); 
    } 
} 

另請參閱:GridView.RowDataBound Event有關MSDN的文檔。