Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/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# datagridview是否显示sql数据库中的选定行?_C#_Database - Fatal编程技术网

C# datagridview是否显示sql数据库中的选定行?

C# datagridview是否显示sql数据库中的选定行?,c#,database,C#,Database,我是c#程序员。目前,我正在尝试在datagrid viewer的sql数据中添加5列中的3列(fileId、filePath、authorName、fileContent、DateSend);fileId和fileContent列是隐藏的。多谢各位 con = new SqlConnection("Data Source=LEO-PC\\SQLEXPRESS;Initial Catalog = datashare;Integrated Security = True");

我是c#程序员。目前,我正在尝试在datagrid viewer的sql数据中添加5列中的3列(fileId、filePath、authorName、fileContent、DateSend);fileId和fileContent列是隐藏的。多谢各位

        con = new SqlConnection("Data Source=LEO-PC\\SQLEXPRESS;Initial Catalog = datashare;Integrated Security = True");
        cmd = new SqlCommand("select * from maintable", con);
        con.Open();
        SqlDataReader sqlRead;
        try
        {
            sqlRead = cmd.ExecuteReader();
            while (sqlRead.Read())
            {
               //Adding to datagrid
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }

要隐藏这两个字段,请在网格中创建OnRowCreated事件:

OnRowCreated=“gridView1\u RowCreated”

记住使用

SqlDataReader reader = cmd.ExecuteReader();     
gridView1.DataSource = reader; 
并且gridview必须具有属性必须是
autogeneratedColumns=“true”

然后按如下方式创建事件:

protected void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
            if (e.Row.RowType == DataControlRowType.Header)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    DataControlFieldCell cell =
                        (DataControlFieldCell) e.Row.Cells[i];


                    if (cell.ContainingField.HeaderText == "fileId")
                    {
                        e.Row.Cells[i].Visible = false;
                        cell.Visible = false;
                    }

                    if (cell.ContainingField.HeaderText == "fileContent")
                    {
                        e.Row.Cells[i].Visible = false;
                        cell.Visible = false;
                    }
                }
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    DataControlFieldCell cell =
                        (DataControlFieldCell) e.Row.Cells[i];

                    if (cell.ContainingField.ToString() == "fileId")
                    {
                        cell.Visible = false;
                    }

                    if (cell.ContainingField.ToString() == "fileContent")
                    {
                        cell.Visible = false;
                    }
                }
            }
}
protectedvoid gridView1\u RowCreated(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Header)
{
for(int i=0;i

这可能不是最有效的方法,但它可以通过使用代码达到目的。绑定列时,应该能够使用gridview模板隐藏列

如果您是初学者,可以检查SQLDataSource控件。