【问题标题】:Child Window Form does not show color scheme on gridview子窗口窗体在 gridview 上不显示配色方案
【发布时间】:2015-12-10 23:27:07
【问题描述】:

我设计了一个表单,它在 datagridview(交替行)上显示配色方案。它工作正常。但是当我从 mdi 父表单菜单条选项卡中调用它时,它不会在 datagridview 上显示颜色(填充表单加载功能)。当我只运行子表单时,它会在 gridview3 和 datagridview4 上显示颜色。像

但是当我从父级调用时,它不会在 datagridview3(备用行)和 datagridview4(备用行)中显示颜色。 看起来像

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.SelectedItem = "Select Gender";

    using (SqlConnection con = new SqlConnection(conn))
    {

        SqlDataAdapter sda = new SqlDataAdapter("getdata", con);
        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataSet ds = new DataSet();
        sda.Fill(ds);
        ds.Tables[0].TableName = "Product";
        ds.Tables[1].TableName = "Category";

        dataGridView3.DataSource = ds.Tables["Product"];
        dataGridView4.DataSource = ds.Tables["Category"];


    }
    gridrowcolor();


}
public void gridrowcolor()
{
    DataGridViewCellStyle st = new DataGridViewCellStyle();
    st.Font = new Font("Arial", 12, FontStyle.Bold);

    for (int i = 0; i < dataGridView4.Rows.Count-1; i++)
    {
        //dataGridView4.RowsDefaultCellStyle.BackColor = Color.Orange;
        //dataGridView4.AlternatingRowsDefaultCellStyle.BackColor = Color.BurlyWood;

        int ii = Convert.ToInt32(dataGridView4.Rows[i].Cells[0].Value.ToString());
        if (ii % 2 == 0)
        {
            dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
            dataGridView4.Rows[i].DefaultCellStyle = st;
        }
        else
            dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Brown;
    }

    for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
    {


        dataGridView3.CellBorderStyle = DataGridViewCellBorderStyle.None;
        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.Font = new Font(dataGridView3.Font, FontStyle.Bold);

        int ii = Convert.ToInt32(dataGridView3.Rows[i].Cells[0].Value.ToString());
        if (ii % 2 == 0)
        {
            dataGridView3.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
            dataGridView3.Rows[i].DefaultCellStyle = style;
            dataGridView3.DefaultCellStyle.SelectionForeColor = Color.Chocolate;
        }
        else
            dataGridView3.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
    }
}


 From Parent MDI Form

private void receiptCancelRequestToolStripMenuItem_Click(object sender, EventArgs e)
{
    Form1 frm = new Form1();
    frm.MdiParent = this;
    frm.Show();

}

【问题讨论】:

    标签: c# winforms gridview


    【解决方案1】:

    假设您已经发布了 ChildForm 的代码,并且您的应用程序设置为 MDI 父级,并且您有数据。您应该使用调用 gridrowcolor(); 的 DataGridViews 的 OnBindingComplete 事件;从那里。这应该可以。

        Form1_Load()
        {
    
        dataGridView4.OnBindingComplete += SetGridViewRows;
        ... // the rest of your code...
    
        }
    
    SetGridViewRows(object sender, BindingCompleteEventArgs e)
    {
    
        DataGridViewCellStyle st = new DataGridViewCellStyle();
        st.Font = new Font("Arial", 12, FontStyle.Bold);
    
        for (int i = 0; i < dataGridView4.Rows.Count-1; i++)
        {
            //dataGridView4.RowsDefaultCellStyle.BackColor = Color.Orange;
            //dataGridView4.AlternatingRowsDefaultCellStyle.BackColor = Color.BurlyWood;
    
            int ii = Convert.ToInt32(dataGridView4.Rows[i].Cells[0].Value.ToString());
            if (ii % 2 == 0)
            {
                dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                dataGridView4.Rows[i].DefaultCellStyle = st;
            }
            else
                dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Brown;
        }
    
    
    }
    

    上面的代码——为了简洁起见,我复制了——你实际上可以使用相同的事件——只使用 sender 和 eventargs 来达到你的目的。 使用 CellFormatEvent - 我不推荐,因为该方法可能会被调用两次,具体取决于您在做什么。 使用 DataGridView 的 BindingCompleteEvent 应该没有问题。

    编辑 好的,我为你写了代码:

    Form1_Load()
    {
        // This method subscribes to the DataGridView Binding Complete Event. Only after DdataBinding is Complete
            // For example when you do this dataGridView3.DataSource = MySource; or dataGridView3.ResetBindings(false);
           dataGridView3.DataBindingComplete += dataGridView3_DataBindingComplete;
    
           // This Method Subscribes to the Cell Formatting event - will be called when formatting the Cells!
        dataGridView3.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView3_CellFormatting);
    
    // All of my other code that I have in the load event..
    
    }
    
    // This is called when Databinding is complete 
            private void dataGridView3_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
            {
                dgBinContent.ClearSelection();
    
                DataGridView myGrid = (DataGridView)sender;
                for (int i = 0; i < myGrid.Rows.Count - 1; i++)
                {
    
    
                    myGrid.CellBorderStyle = DataGridViewCellBorderStyle.None;
                    DataGridViewCellStyle style = new DataGridViewCellStyle();
                    style.Font = new System.Drawing.Font(myGrid.Font, System.Drawing.FontStyle.Bold);
    
                    int ii = Convert.ToInt32(myGrid.Rows[i].Cells[0].Value.ToString());
                    if (ii % 2 == 0)
                    {
                        myGrid.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
                        myGrid.Rows[i].DefaultCellStyle = style;
                        myGrid.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Chocolate;
                    }
                    else
            {
                        myGrid.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Orange;
            }
                }
    
             }
    
    // This is called when Cell Formatting
            void dataGridView3_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                this.dataGridView3.Rows[e.RowIndex].HeaderCell.Value = String.Format("{0}", e.RowIndex + 1);
    
                dataGridView3.CellBorderStyle = DataGridViewCellBorderStyle.None;
                DataGridViewCellStyle style = new DataGridViewCellStyle();
                style.Font = new System.Drawing.Font(dataGridView3.Font, System.Drawing.FontStyle.Bold);
    
                int ii = Convert.ToInt32(dataGridView3.Rows[e.RowIndex].Cells[0].Value.ToString());
                if (ii % 2 == 0)
                {
                    dataGridView3.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
                    dataGridView3.Rows[e.RowIndex].DefaultCellStyle = style;
                    dataGridView3.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Chocolate;
                }
                else
                {
                    dataGridView3.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Orange;
                }
    
            }
    

    【讨论】:

    • 给定的代码是 childform 并且正在调用(childform.mdiparent=this)。而不是在 datagridview 上显示配色方案
    • @Farhat Ullah - 我用代码编辑了我的回复 - 该代码是为 dataGridView3 设置的,如果你需要它 4 - 你可以使用相同的代码复制和粘贴,只需将 dataGridView3 更改为 dataGridView4方法调用和订阅。您只需要其中一种方法,databindingComplete 或 CellFormatting。如果 DatabindingComplete 不起作用,您可能只是订阅 DataGridview 的 BindingContextChanged。
    • @Farhat Ullah - 你在行中的值是 NULL (异常中的 SQLDataAdapter 声明它是值) - 它不能转换为字符串 - 你需要验证值不是 NULL ;如果您想要默认值,请使用合并运算符?或者 ?? .示例 int ii = Convert.ToInt32(dataGridView4.Rows[i].Cells[0].Value.ToString()) ?? 0;请参阅此链接了解更多信息。 msdn.microsoft.com/en-us/library/ms173224.aspx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    相关资源
    最近更新 更多