2012年8月7日 星期二

[C#]自動產生DataGridView

自動產生DataGridView


DataTable dt = GetScoreTypeTable();
//取得項目類別

DataTable dtItem = AssessScoreProxy.GetAssessItem(101, 8);
foreach (DataRow dr in dt.Rows)
{

    TabPage tbpage = new TabPage();
    tbpage.Location = new System.Drawing.Point(4, 24);
    tbpage.Padding = new System.Windows.Forms.Padding(3);
    tbpage.Size = new System.Drawing.Size(685, 539);
    tbpage.Name = "tp" + dr["No"];
    tbpage.Text = dr["Name"].ToString();
    tbpage.TabIndex = Convert.ToInt16(dr["No"]);
    tbpage.AutoScroll = true;
//是否自動顯示捲軸

    tbpage.UseVisualStyleBackColor = true;
    tabControl1.Controls.Add(tbpage);

    DataGridView dgv = new DataGridView();
    DataGridViewTextBoxColumn colItem = new DataGridViewTextBoxColumn();
    DataGridViewTextBoxColumn colScore = new DataGridViewTextBoxColumn();
    DataGridViewTextBoxColumn colTotScore = new DataGridViewTextBoxColumn();
    DataGridViewTextBoxColumn colScored = new DataGridViewTextBoxColumn();

    dgv.AllowUserToAddRows = false;
    dgv.AllowUserToDeleteRows = false;

    dgv.BackgroundColor = System.Drawing.SystemColors.Window;
    dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    dgv.Columns.Add(colItem);
    dgv.Columns.Add(colScore);
    dgv.Columns.Add(colTotScore);
    dgv.Columns.Add(colScored);
    dgv.Dock = System.Windows.Forms.DockStyle.Fill;
    dgv.Location = new System.Drawing.Point(0, 0);
    dgv.Name = "dgv";
    dgv.RowHeadersWidth = 10;
    dgv.RowTemplate.Height = 24;
    dgv.Size = new System.Drawing.Size(685, 539);
    dgv.TabIndex = 0;

    // colItem 
    colItem.HeaderText = "項目";
    colItem.Name = "colItem" + dr["No"];
    colItem.ReadOnly = true;
    colItem.Width = 150;
    colItem.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
    // colScore
    colScore.HeaderText = "分數";
    colScore.Name = "colScore" + dr["No"];

    // colTotScore
    colTotScore.HeaderText = "此項目所佔總分";
    colTotScore.Name = "colTotScore" + dr["No"];
    colTotScore.ReadOnly = true;
    colTotScore.Width = 150;

    // colScored
    colScored.HeaderText = "此項目已評分數加總";
    colScored.Name = "colScored" + dr["No"];
    colScored.ReadOnly = true;
    colScored.Width = 180;

    DataRow[] drItem = dtItem.Select("ScoreType='" + dr["No"] + "'");
    foreach (DataRow drI in drItem)
    {
        dgv.Rows.Add(drI["ScoreItemName"], "", drI["Percentage"], 0);
    }
    tbpage.Controls.Add(dgv);
}


沒有留言:

張貼留言