繁体   English   中英

在asp.net中计算GridView列的值

[英]calculate gridview column value in asp.net

我希望第三个gridview列值取决于第一列值。我的代码如下:

  protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            GridViewRow item = e.Row;
            string selectSQL = "  SELECT fail,COUNT(*) AS 'Count' FROM Table WHERE Id =" + item.Cells[0].Text;
            SqlConnection con = new SqlConnection(connectionstring.ToString());
            SqlCommand cmd = new SqlCommand(selectSQL, con);
            SqlDataReader reader;
            try
            {   con.Open();
                reader = cmd.ExecuteReader();
                reader.Read();

                if (reader["Count"].Equals("0"))
                    item.Cells[3].Text = "0";
                else
                    item.Cells[3].Text = reader["Count"].ToString();
                reader.Close();
            }
            catch (Exception err)
            { }
            finally
            {
                con.Close();
            }


        }
    }

编辑:

GridViewRow item = e.Row;
            int myvar;
            Int32.TryParse(item.Cells[0].Text, out myvar);
            string selectSQL = "  SELECT COUNT(*) AS 'Count' FROM Table WHERE Id=@myvar group by Id";

我可以这样做吗? 我的代码没有任何变化。

我尝试调试..控件进入catch块..不知道我在哪里错

您正在RowDataBound内部调用数据库。 假设您有数千行,则数据库将被调用数千次。

  • 您的第一个查询易于进行SQL injection

要解决这些问题,首先您必须为所有Ids获取“ COUNTS ”并将其存储在本地变量(如Dictionary)中。 然后在RowDataBound期间,您可以使用局部变量中的数据。

上面的场景我已经用一些示例数据进行了模拟。

这是我在Page级声明的局部变量,

    private Dictionary<int, int> dictionaryIds = new Dictionary<int, int>();

这是我的Page_Load方法,

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

这是我的BindData方法,

    private void BindData()
    {
        DataSet ds = new DataSet();

        using (SqlConnection connection = new SqlConnection(connectionstring.ToString()))
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Table", connection))
            {
                adapter.Fill(ds);
            }
        }

        using (SqlConnection connection = new SqlConnection(connectionstring.ToString()))
        {
            using (SqlCommand command = new SqlCommand("SELECT COUNT(*) AS 'Count' FROM Table", connection))
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    dictionaryIds.Add(Convert.ToInt32(reader["Id"].ToString()), Convert.ToInt32(reader["Count"].ToString()));
                }
                connection.Close();

            }
        }

        GridView2.DataSource = ds;
        GridView2.DataBind();
    }

这是我的RowDataBound方法,

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow item = e.Row;
            int myvar;
            Int32.TryParse(item.Cells[0].Text, out myvar);

            if (dictionaryIds.ContainsKey(myvar))
                item.Cells[3].Text = dictionaryIds[myvar].ToString();
        }
    }

另一个建议是,您必须将数据提取逻辑移到数据访问层。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM