如何在数据网格视图中包含信息,如何将按钮中的文本设置为粗体

时间:2012-07-27 04:13:53

标签: c# datagridview

如果数据网格视图中包含信息,如何将按钮中的文本设置为粗体。

下面是一个示例项目 - 创建一个Windows应用程序并将此代码放入。

我想要做的是,如果某个字段中包含信息,我想只将该单元格按钮字体设为粗体。

e.g。基于下面代码中的数据表

Row 1                    David                     Notes this row bold text in button

Row 2                    Sam                       Notes this row not

Row 3                    Christoff                 Notes this row not

Row 4                    Janet                     Notes this row bold text in button

Row 5                    Melanie                   Notes this row not

代码:

private void Form1_Load(object sender, EventArgs e) {
    DataTable table = GetTable();

    DataGridViewColumn col = new DataGridViewTextBoxColumn();
    col.HeaderText = "Dosage";
    col.Width = 80;
    int colIndex = dataGridView1.Columns.Add(col);

    DataGridViewColumn col2 = new DataGridViewTextBoxColumn();
    col2.HeaderText = "Drug";
    col2.Width = 75;
    colIndex = dataGridView1.Columns.Add(col2);

    DataGridViewColumn col3 = new DataGridViewTextBoxColumn();
    col3.HeaderText = "Patient";
    col3.Width = 75;
    colIndex = dataGridView1.Columns.Add(col3);

    DataGridViewColumn col4 = new DataGridViewTextBoxColumn();
    col4.HeaderText = "Date";
    col4.Width = 40;
    colIndex = dataGridView1.Columns.Add(col4);

    DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();
    buttonCol.Name = "btnNotes";
    buttonCol.HeaderText = "Notes";
    buttonCol.Text = "Notes";
    buttonCol.Width = 80;
    buttonCol.UseColumnTextForButtonValue = true;
    buttonCol.DefaultCellStyle.Font = new Font("Arial", 12, FontStyle.Bold);
    dataGridView1.Columns.Add(buttonCol);

    // Add items to the grid
    int i = 0;

    foreach(DataRow rows in table.Rows) {
        dataGridView1.Rows.Add();
        dataGridView1[1, i].Value = rows[0].ToString();
        dataGridView1[1, i].Value = rows[1].ToString();
        dataGridView1[2, i].Value = rows[2].ToString();
        dataGridView1[3, i].Value = rows[3].ToString();

        if (rows[3].ToString().Trim().Length != 0) {

            //Because there are notes in this field, I would like to set this button text only to bold
        }


        i++;
    }

}

static DataTable GetTable() {
    DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(string));
    table.Columns.Add("BlaBlaBla", typeof(string));

    table.Rows.Add(25, "Indocin", "David", "Notes in here");
    table.Rows.Add(50, "Enebrel", "Sam", "");
    table.Rows.Add(10, "Hydralazine", "Christoff", "");
    table.Rows.Add(21, "Combivent", "Janet", "Notes in here");
    table.Rows.Add(100, "Dilantin", "Melanie", "");

    return table;    
}

3 个答案:

答案 0 :(得分:2)

希望这会对你有所帮助。我创建了一个更新按钮样式的计时器。

        DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();
        public Form1()
        {
            InitializeComponent();

            DataGridViewColumn col = new DataGridViewTextBoxColumn();
            col.HeaderText = "Dosage";
            col.Width = 80;
            int colIndex = dataGridView1.Columns.Add(col);

            DataGridViewColumn col2 = new DataGridViewTextBoxColumn();
            col2.HeaderText = "Drug";
            col2.Width = 75;
            colIndex = dataGridView1.Columns.Add(col2);

            DataGridViewColumn col3 = new DataGridViewTextBoxColumn();
            col3.HeaderText = "Patient";
            col3.Width = 75;
            colIndex = dataGridView1.Columns.Add(col3);

            DataGridViewColumn col4 = new DataGridViewTextBoxColumn();
            col4.HeaderText = "Date";
            col4.Width = 40;
            colIndex = dataGridView1.Columns.Add(col4);


            buttonCol.Name = "btnNotes";
            buttonCol.HeaderText = "Notes";
            buttonCol.Text = "Notes";
            buttonCol.Width = 80;
            buttonCol.UseColumnTextForButtonValue = true;
            buttonCol.DefaultCellStyle.Font = new Font("Arial", 12);
            dataGridView1.Columns.Add(buttonCol);

            timer1.Start();
            timer1.Tick += new EventHandler(timer1_Tick);


        }

        void timer1_Tick(object sender, EventArgs e)
        {
            int i = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Value == null || row.Cells[1].Value == null || row.Cells[2].Value == null || row.Cells[3].Value == null)
                {
                    dataGridView1.Rows[i].Cells[4].Style.Font = new Font(dataGridView1.Font, FontStyle.Regular);
                }
                else
                {
                    dataGridView1.Rows[i].Cells[4].Style.Font = new Font(dataGridView1.Font, FontStyle.Bold);
                }
                i++;
            }
        }



        static DataTable GetTable()
        {
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("BlaBlaBla", typeof(string));

        table.Rows.Add(25, "Indocin", "David", "Notes in here");
        table.Rows.Add(50, "Enebrel", "Sam", "");
        table.Rows.Add(10, "Hydralazine", "Christoff", "");
        table.Rows.Add(21, "Combivent", "Janet", "Notes in here");
        table.Rows.Add(100, "Dilantin", "Melanie", "");

        return table;
        }

答案 1 :(得分:2)

检查数据表中绑定到gridview的数据

如果它包含任何数据 即。

gridview.datasource = datatable;
gridview.databind();
if(datatable.rows.count != 0)
{
 button.Font.Bold = true;
}
else
{
 button.Font.Bold = false;
}

答案 2 :(得分:0)

Button btn = (Button)datagridview1.FindControl("Button1");

if(datatable.rows.count != 0)
{ 
  button.Font.Bold = true; 
} 
else 
{ 
  button.Font.Bold = false;
}