0

私は c# プログラマーです。現在、datagrid-viewer の sql データに 5 つの列 (fileId、filePath、authorName、fileContent、DateSend) のうち 3 つを追加しようとしています。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();
        }
4

1 に答える 1

0

2つのフィールドを非表示にするには、グリッドにOnRowCreatedイベントを作成します。

OnRowCreated = "gridView1_RowCreated"

を使用してグリッドにデータを入力することを忘れないでください

SqlDataReader reader = cmd.ExecuteReader();     
gridView1.DataSource = reader; 

グリッドビューにはプロパティが必要ですAutoGeneretedColumns = "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;
                    }
                }
            }
}

これは最も効率的な方法ではないかもしれませんが、コードを使用する目的には役立ちます。列がバインドされているときに、gridviewテンプレートを使用して列を非表示にできるはずです。

于 2012-08-15T06:57:19.973 に答える