如何验证单元格数据 [英] How to validate cell data

查看:70
本文介绍了如何验证单元格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我有一个datagridview,显示学生ID,学生姓名和年级以及2列,标题文本为最终成绩和备注。现在,如果用户在列最终成绩输入学生的最终成绩,则如果学生通过或失败,则列备注将自动显示。我所做的是在datagridview中添加最终成绩,但我不能做后一部分。谁能帮我?在此先感谢。





例如:



StudentID || StudentName || GradeLevel || FinalGrade ||备注

--------------------------------- ----------------------

20110876 || John Lee || 1 || 89 ||通过

---------------------------------

20110877 || Pete Low || 1 || 75 ||通过

---------------------------------

20110878 || Chris Tiu || 1 || 74 ||失败

解决方案

听起来你需要在单元格上处理文本更改事件gridview上的CellStateChanged事件。



我更喜欢CellStateChanged,因为它在按Enter键后进行计算(单元格失去焦点)。



这假设FinalGrade列的名称是FinalGrade,而不仅仅是文本。

  private   void  CellChanged( object  sender,DataGridViewC ellStateChangedEventArgs e){
if (e.Cell.OwningColumn.Name == FinalGrade){
if null != e.Cell.Value){
int score = 0 ;
if int .TryParse(e.Cell.Value.ToString(), out 得分)){
// dgv1是你的数据网格视图。
DataGridViewCell cell = dgv1.Rows [e.Cell.RowIndex] .Cells [ 备注];
if null != cell){
if (得分> = 75 ){
cell .Value = 通过;
}
else {
cell.Value = 失败;
}
}
}
}
}
}


Hi guys.

I have a datagridview that display the student ID, student Name and gradelevel and 2 columns with header text as Final Grade and Remarks. Now, if the user inputs the final grades for the student at Column Final Grade, the Column Remarks will automatically display if the student is Passed or Failed. What I have done is adding the final grade in datagridview but I cannot do the latter part. Can anyone help me? Thanks in advance.


EXAMPLE:

StudentID||StudentName||GradeLevel||FinalGrade||Remarks
-------------------------------------------------------
20110876||John Lee||1||89||Passed
---------------------------------
20110877||Pete Low||1||75||Passed
---------------------------------
20110878||Chris Tiu||1||74||Failed

解决方案

Sounds like you need to handle a text changed event on the cell or the CellStateChanged event on the gridview.

I prefer the CellStateChanged as it does calculations after pressing enter (cell loses focus).

This assumes the name of the FinalGrade column is FinalGrade, not just the text.

private void CellChanged(object sender, DataGridViewCellStateChangedEventArgs e) {
    if (e.Cell.OwningColumn.Name == "FinalGrade") {
        if (null != e.Cell.Value) {
            int score = 0;
            if (int.TryParse(e.Cell.Value.ToString(), out score)) {
                // dgv1 is your data grid view.
                DataGridViewCell cell = dgv1.Rows[e.Cell.RowIndex].Cells["Remarks"];
                if (null != cell) {
                    if (score >= 75) {
                        cell.Value = "Passed";
                    }
                    else {
                        cell.Value = "Failed";
                    }
                }
            }
        }
    }
}


这篇关于如何验证单元格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆