Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我有一个由查询自动生成的gridview。。我需要访问gridview中的值,以便在下一个gridview中进行进一步计算_C#_Asp.net - Fatal编程技术网

C# 我有一个由查询自动生成的gridview。。我需要访问gridview中的值,以便在下一个gridview中进行进一步计算

C# 我有一个由查询自动生成的gridview。。我需要访问gridview中的值,以便在下一个gridview中进行进一步计算,c#,asp.net,C#,Asp.net,我想使用此gridview值来计算。。如何访问网格中的值。。帮助我..通过基础数据集而不是通过GridView访问值会更容易。即: con.Open(); string str = DropDownList1.SelectedValue; SqlDataAdapter da = new SqlDataAdapter(@" select distinct a.DepotCode, a.CustWt, b.CustName, b.Lat_Degr

我想使用此gridview值来计算。。如何访问网格中的值。。帮助我..

通过基础数据集而不是通过GridView访问值会更容易。即:

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();
您可以使用数据集变量ds访问实际数据

然后是:

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

在您的场景中有很多方法,但简单的答案是在绑定到GridView1后在dataset ds中的datatable上循环,然后将该表绑定到其他gridview

在将数据集绑定到gridview之前,更容易使用数据集本身中的数据进行计算

但是,如果要访问网格中的项,可以在服务器控件绑定到数据源后使用gridview RowDataBound事件执行某些操作

如果页面中有此GridView:

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';
}
另请参见:有关MSDN的文档

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
              OnRowDataBound="GridView1_RowDataBound" />
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();
  }
}