访问网格视图中的单元格
上周我开始尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要像这样在 DataGridView 上使用 CellFormatting 事件
You need to use the CellFormatting Event on the DataGridView like so