国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何于DataGridView控件中以跨數據行方式顯示數據

2019-11-18 16:56:28
字體:
來源:轉載
供稿:網友

一般來說,每一個字段的內容會單獨顯示于DataGridView控件的一個數據行中。問題是,某些字段擁有大量文字數據,我是不是能夠讓該字段的內容以跨數據行的方式來顯示,以便在有限的畫面空間中的呈現出更完整的內容呢?答案當然是肯定的。


以圖表1所示的執行畫面而言,「自傳」字段的內容并未單獨顯示于一個數據行中,而是以橫跨數據行的方式,顯示在同筆數據列之各字段內容的下方。相關程序代碼列示如下:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;



PRivate int oldRowIndex = 0;
private const int CUSTOM_CONTENT_HEIGHT = 80;
private DataSet myDataSet;

private void CH13_DemoForm009_Load(object sender, EventArgs e)
{
    Padding newPadding = new Padding(0, 1, 0, CUSTOM_CONTENT_HEIGHT);
    this.DataGridView1.RowTemplate.DefaultCellStyle.Padding = newPadding;

    this.DataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor =
        Color.Transparent;

    this.DataGridView1.RowTemplate.Height += CUSTOM_CONTENT_HEIGHT;

    this.DataGridView1.AllowUserToAddRows = false;
    this.DataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
    this.DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
    this.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

    myDataSet = LoadDataToDataSet();

    if(myDataSet != null)
    {
        // 將 BindingSource 組件系結至數據集對象中的「飛狐工作室」數據表。
        this.BindingSource1.DataMember = "飛狐工作室";
        this.BindingSource1.DataSource = myDataSet;

        this.BindingSource1.AllowNew = false;

        // 將 BindingNavigator 控件的數據來源也設定成 BindingSource 組件
        //,如此一來,就可以使用 BindingNavigator 控件去導覽
        // DataGridView 控件中的數據列。
        this.BindingNavigator1.BindingSource = this.BindingSource1;

        this.DataGridView1.DataSource = this.BindingSource1;
    }
    else
    {
        return;
    }

    this.DataGridView1.Columns[4].Visible = false;

    this.DataGridView1.Columns[0].SortMode =
         DataGridViewColumnSortMode.NotSortable;
    this.DataGridView1.Columns[2].SortMode =
         DataGridViewColumnSortMode.NotSortable;
    this.DataGridView1.Columns[3].SortMode =
         DataGridViewColumnSortMode.NotSortable;

 

 


    this.DataGridView1.AutoResizeRows(
        DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders);
}

private void DataGridView1_ColumnWidthChanged(object sender,
                                         DataGridViewColumnEventArgs e)
{
    this.DataGridView1.Invalidate();
}

private void DataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
    if(oldRowIndex != -1)
    {
        this.DataGridView1.InvalidateRow(oldRowIndex);
    }

    oldRowIndex = this.DataGridView1.CurrentCellAddress.Y;
}

private void DataGridView1_RowPrePaint(object sender,
                            DataGridViewRowPrePaintEventArgs e)
{
    e.PaintParts = e.PaintParts & (~DataGridViewPaintParts.Focus);

    if((e.State & DataGridViewElementStates.Selected) ==
                                DataGridViewElementStates.Selected)
    {
        Rectangle rowBounds = new Rectangle(
            this.DataGridView1.RowHeadersWidth, e.RowBounds.Top,
            this.DataGridView1.Columns.GetColumnsWidth(
            DataGridViewElementStates.Visible) -
            this.DataGridView1.HorizontalScrollingOffset + 1,
            e.RowBounds.Height);

        System.Drawing.Drawing2D.LinearGradientBrush backbrush =
            new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
            this.DataGridView1.DefaultCellStyle.SelectionBackColor,
            e.InheritedRowStyle.ForeColor,
            System.Drawing.Drawing2D.LinearGradientMode.Horizontal);

        try
        {
            e.Graphics.FillRectangle(backbrush, rowBounds);
        }
        finally
        {
            backbrush.Dispose();
        }
    }
}

private void DataGridView1_RowPostPaint(object sender,
                               DataGridViewRowPostPaintEventArgs e)
{
    Rectangle rowBounds = new Rectangle(this.DataGridView1.RowHeadersWidth,
        e.RowBounds.Top, this.DataGridView1.Columns.GetColumnsWidth(
   
    DataGridViewElementStates.Visible) -
        this.DataGridView1.HorizontalScrollingOffset + 1, e.RowBounds.Height);

    SolidBrush forebrush = null;

    try
    {
        if((e.State & DataGridViewElementStates.Selected) ==
            DataGridViewElementStates.Selected)
        {
            forebrush = new SolidBrush(e.InheritedRowStyle.SelectionForeColor);
        }
        else
        {
            forebrush = new SolidBrush(e.InheritedRowStyle.ForeColor);
        }

        Object recipe =
          this.DataGridView1.Rows.SharedRow(e.RowIndex).Cells[4].Value;

        if(!(recipe == null))
        {
            string text = recipe.ToString();
            Rectangle textArea = rowBounds;
            RectangleF clip = textArea;

            textArea.X -= this.DataGridView1.HorizontalScrollingOffset;
            textArea.Width += this.DataGridView1.HorizontalScrollingOffset;
            textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom;
            textArea.Height -= rowBounds.Height -
                                   e.InheritedRowStyle.Padding.Bottom;
            textArea.Height =
               (textArea.Height / e.InheritedRowStyle.Font.Height) *
                e.InheritedRowStyle.Font.Height;
           
            clip.Width -= this.DataGridView1.RowHeadersWidth + 1 - clip.X;
            clip.X = this.DataGridView1.RowHeadersWidth + 1;
                  
            RectangleF oldClip = e.Graphics.ClipBounds;

            e.Graphics.SetClip(clip);

            e.Graphics.DrawString(text, e.InheritedRowStyle.Font,
                                  forebrush, textArea);

            e.Graphics.SetClip(oldClip);
        }
    }
    finally
    {
        forebrush.Dispose();
    }

    if (this.DataGridView1.CurrentCellAddress.Y == e.RowIndex)
    {
        e.DrawFocus(rowBounds, true);
    }
}

private void DataGridView1_RowHeightChanged(
                    object sender, DataGridViewRowEventArgs e)
{
    int preferredNormalContentHeight =
       e.Row.GetPreferredHeight(e.Row.Index,
        DataGridViewAutoSizeRowMode.AllCellsExceptHeader, true) -
        e.Row.DefaultCellStyle.Padding.Bottom;

    Padding newPadding = e.Row.DefaultCellStyle.Padding;
           
    newPadding.Bottom = e.Row.Height - preferredNormalContentHeight;
    e.Row.DefaultCellStyle.Padding = newPadding;
}

// 本程序會連接至數據來源并建立所需的 DataSet 對象。
private DataSet LoadDataToDataSet()
{
    // 利用 SqlConnectionStringBuilder 對象來構建連接字符串。
    SqlConnectionStringBuilder sqlStringBuilder =
        new SqlConnectionStringBuilder();

    sqlStringBuilder.DataSource = @"(local)/SQLEXPRESS";
    sqlStringBuilder.InitialCatalog = "北風貿易";
    sqlStringBuilder.IntegratedSecurity = true;

    // 建立一個數據集。
    DataSet ds = new DataSet();

    try
    {
        using (SqlConnection northwindConnection =
            new SqlConnection(sqlStringBuilder.ConnectionString))
        {
            SqlCommand cmdLiming = new SqlCommand(
              "SELECT 姓名,員工性別,出生日期, 目前薪資, 自傳" +
              " FROM dbo.飛狐工作室 WHERE 自傳 IS NOT NULL",
              northwindConnection);

            northwindConnection.Open();

            using (SqlDataReader drLiming = cmdLiming.ExecuteReader())
            {
                ds.Load(
                  drLiming,
                  LoadOption.OverwriteChanges,
                  new string[] { "飛狐工作室" });
            }
        }
    }
    catch (Exception)
    {
        MessageBox.Show(
            "要能夠順利執行本范例程序,您的計算機必須已安裝 SQL Server " +
            "Express,并且必須已附加了本書所附的「北風貿易」數據庫。" +
            "關于如何安裝 SQL Server Express,請參閱附錄或相關文件說明。");

        // 無法連接至 SQL Server。
        return null;
    }

    return ds;
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 敖汉旗| 西峡县| 辽中县| 高雄县| 确山县| 普洱| 盐亭县| 新巴尔虎左旗| 古田县| 西峡县| 阿克陶县| 鄂温| 稻城县| 马鞍山市| 武川县| 青田县| 台东县| 南部县| 青神县| 伊通| 泽普县| 静乐县| 彩票| 彭山县| 大同县| 景泰县| 沾化县| 宝鸡市| 萨迦县| 新营市| 寻乌县| 东港市| 南汇区| 鹤山市| 寻甸| 嘉定区| 平罗县| 专栏| 塔河县| 大理市| 乌海市|