从datagridview隐藏id列但需要使用它

时间:2016-02-29 19:30:35

标签: c# datagridview

我想隐藏datagridview中的id列。我尝试通过更改存储过程/ SQL查询,它工作,但由于某些原因我无法获得该条目的ID我还需要每个条目的ID,但不想在前端显示。

有什么建议吗?

DECLARE @my_int INT = 5

;WITH CTE_Binary AS
(
    SELECT 1 AS seq, 1 AS val
    UNION ALL
    SELECT seq + 1 AS seq, power(2, seq)
    FROM CTE_Binary
    WHERE
        seq < 8
)
SELECT
(
    SELECT
        CAST(CASE WHEN B2.seq IS NOT NULL THEN 1 ELSE 0 END AS CHAR(1))
    FROM
        CTE_Binary B1
    LEFT OUTER JOIN CTE_Binary B2 ON
        B2.seq = B1.seq AND
        @my_int & B2.val = B2.val
    ORDER BY
        B1.seq DESC
    FOR XML PATH('')
) AS val

2 个答案:

答案 0 :(得分:1)

正如评论所说,你可以这样做:

dgName.Columns[i].Visible = false;

或者,如果您绑定到类,则可以在数据值上设置自定义属性,如下所示:

public class Row {
   [Browsable(false)]
   public int Id {get;set;}
   public string Name {get;set;}
}
//dgName.DataSource = new BindingList<Row>();

*编辑:显然你不想绑定到一个空列表......但是你填充你的列表,这只是显示了如何使用Browsable属性。

答案 1 :(得分:1)

首先绑定网格然后尝试此代码。

            for (int i = 0; i < LoadIDcount; i++)
        {
            string IDnow = LoadID[i, 0];
            string IDsaved = LoadP_dt.Rows[i][0].ToString();

            if (LoadP_dt.Rows[i][0].ToString() == IDnow)
            {
                for (int j = 0; j < 8760; j++)
                {
                    string colPnow = SP_dt.Columns[j * 2 + 4].ColumnName.ToString();
                    double ColP_sum = (double)SP_dt.Compute(String.Format("Sum([{0}])", colPnow), String.Format("Load_ID = '{0}'", IDnow));
                    string colQnow = SP_dt.Columns[j * 2 + 5].ColumnName.ToString();
                    double ColQ_sum = (double)SP_dt.Compute(String.Format("Sum([{0}])", colQnow), String.Format("Load_ID = '{0}'", IDnow));


                    LoadP_dt.Rows[i][j + 2] = ColP_sum;
                    LoadQ_dt.Rows[i][j + 2] = ColQ_sum;
                    Console.WriteLine("{0}    {1}", i, j);
                }
            }
            else
            {
                Console.WriteLine("ID does not match");
            }

        }

        CSVfilewriter(CSVPpath, LoadP_dt);//save the Load_P datatable to CSV file
        CSVfilewriter(CSVQpath, LoadQ_dt);//save the Load_Q datatable to CSV file
        //CSVfilewriter(CSVSPpath, SP_dt);//save the service point datatable to CSV file



    }

参考:More Details