访问网格视图中的单元格

发布于 2024-12-08 01:18:46 字数 744 浏览 1 评论 0原文

上周我开始尝试使用 Winforms 来自动化一些报告。我不喜欢这种转变(对你们中的一些人来说可能很简单)!

我当前正在显示使用 Oracle SQL Developer 创建的 SQL 视图中的数据。我通过使用 DataGridView 任务来选择表并显示它来访问数据(这似乎是最简单的方法)。

所有数据都显示正常。但是,我现在想访问表中的单元格。我希望能够根据单元格的值更改单元格的颜色,并在需要时提取一些数据。

要访问数据并突出显示行,我希望做这样的事情

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int theValue = Convert.ToInt32(e.Row.Cells[0].Text);

            if (theValue>0)
                e.Row.Cells[0].BackColor = System.Drawing.Color.Red
    }
}

它不起作用,因为事件没有触发。我想我应该将数据绑定到我的网格视图,但我已经通过设计器加载了所有数据。这是问题所在吗?我不知道如何自己编码,我之前尝试过,但它不稳定。

此外,当我尝试通过设计器更改数据库的 SQL 查询时,它不起作用。我觉得再这样下去我会失去理智的!

Last week I started attempting to use Winforms to automate some reports. I am not enjoying the transition (as simple as it may be to some of you)!

I am currently displaying data from a SQL view that I created using Oracle SQL Developer. I accecssed the data by using the DataGridView Tasks to select the table and display it (it seemed like the most straightforward method).

All of the data displays fine. However, I would now like to access the cells in the table. I want to be able to alter the colours of the cells based upon their values and to pull out some of the data if needs be.

To access the data and highlight the rows I was hoping to do something like this

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int theValue = Convert.ToInt32(e.Row.Cells[0].Text);

            if (theValue>0)
                e.Row.Cells[0].BackColor = System.Drawing.Color.Red
    }
}

It doesn't work because the event is not firing. I think I am supposed to bind the data to my gridview but I have loaded all of the data through the designer. Is this the problem? I have no idea how to code it myself, I tried before but it threw a wobbly.

Furthermore, when I try to change the SQL query for the database through the designer it does not work. I think I am going to lose my mind at this rate!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2024-12-15 01:18:46

您需要像这样在 DataGridView 上使用 CellFormatting 事件

private void MyGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((int)MyGridView.Rows[e.RowIndex].Cells[0].Value > 30)
            {
                e.CellStyle.BackColor = System.Drawing.Color.Red;
                e.CellStyle.ForeColor = System.Drawing.Color.White;
            }
        }

You need to use the CellFormatting Event on the DataGridView like so

private void MyGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((int)MyGridView.Rows[e.RowIndex].Cells[0].Value > 30)
            {
                e.CellStyle.BackColor = System.Drawing.Color.Red;
                e.CellStyle.ForeColor = System.Drawing.Color.White;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文