如何办理登机手续string.IsNullOrEmpty和IsDateEmpty所有可见单元格的行 [英] How to check in all visible rows CELLs for string.IsNullOrEmpty and IsDateEmpty

查看:137
本文介绍了如何办理登机手续string.IsNullOrEmpty和IsDateEmpty所有可见单元格的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有行,我与显示按钮的Click事件/隐藏的表。我想检查我与string.IsNullOrEmpty和IsDateEmpty细胞指数显示的所有列。 如何做到这一点?

在这里输入的形象描述

以下code(对于检查可见细胞[0]文本框)不工作:

  VAR allVisibleRows = myTbl.Rows.Cast<&的TableRow GT;()式(行=> row.Visible)。
布尔anyTextBoxEmpty = allVisibleRows.Any(行=> string.IsNullOrEmpty(((文本框)row.Cells [0] .Controls [0])文本));
// DestinavionValidation
如果(anyTextBoxEmpty)
{
    返回请插入一个文本;
}

以下code(产检第二细胞 1 DateTimeControl )不工作:

 布尔anyDateTimeOneValid = allVisibleRows.Any(行=>!(((DateTimeControl)row.Cells [1] .Controls [0]))的IsValid);
布尔anyDateTimeOneEmpty = allVisibleRows.Any(行= GT;(((DateTimeControl)row.Cells [1] .Controls [0]))IsDateEmpty);
//日期验证
如果(anyDateTimeOneValid || anyDateTimeOneEmpty)
{
    返回请插入日期!;
}

这是下面的错误


  

System.ArgumentOutOfRangeException:指定的参数已超出有效值的范围。参数名:指数System.Web.UI.ControlCollection.get_Item(的Int32指数)在Lirex.WayBillModule.b__2f(的TableRow行)在System.Linq.Enumerable.Any [TSource](IEnumerable的 1来源, FUNC 2 predicate)



解决方案

您可以试试下面如果会的工作,它会检查空文本框上的所有可见行,只需修改检查其他控制类型。

  VAR tableRows = Table1.Rows.Cast<&的TableRow GT;()式(行=> row.Visible)。        布尔hasEmptyField = FALSE;
        的foreach(VAR行tableRows.Where(行=> row.Cells.Cast< TableCell的>()
            .SelectMany(
                项目= GT; item.Controls.Cast<控制>()
                    。凡(CNTRL => cntrl.GetType()== typeof运算(文本框)))
            。任何(CNTRL = GT; string.IsNullOrEmpty(((文本框)CNTRL)。文本))))
        {
            hasEmptyField = TRUE;
            打破;
        }        如果(hasEmptyField)
        {
            //你想要什么?
        }

编辑答案。最近一次操作是只检查上次可见行。在得到可视行..变化也包括对每行循环做了一些改变,现在使用WHERE子句,而不是最后一次。

以下是原始code我张贴检查的最后一个可见行只。

  VAR的TableRow = Table1.Rows.Cast<&的TableRow GT;()上次(行=> row.Visible)。
        VAR hasEmptyTextBox =
            tableRow.Cells.Cast< TableCell的>()
                .SelectMany(
                    项目= GT; item.Controls.Cast<控制>()
                        。凡(CNTRL => cntrl.GetType()== typeof运算(文本框)))
                。任何(CNTRL => string.IsNullOrEmpty(((文本框)CNTRL)。文本));

I have a table with rows where I show/hide with button click event. I want to check for my all visible rows with cell index for string.IsNullOrEmpty and IsDateEmpty. How to do this?

The following code(for check visible cell[0] TextBox) not working:

var allVisibleRows = myTbl.Rows.Cast<TableRow>().Where(row => row.Visible);
bool anyTextBoxEmpty = allVisibleRows.Any(row => string.IsNullOrEmpty(((TextBox)row.Cells[0].Controls[0]).Text));
//DestinavionValidation
if (anyTextBoxEmpty)
{
    return "Please, insert a TEXT";
}

The following code(for check second Cell 1 DateTimeControl) not working:

bool anyDateTimeOneValid = allVisibleRows.Any(row => !(((DateTimeControl)row.Cells[1].Controls[0])).IsValid);
bool anyDateTimeOneEmpty = allVisibleRows.Any(row => (((DateTimeControl)row.Cells[1].Controls[0])).IsDateEmpty);
//Date Validation
if (anyDateTimeOneValid || anyDateTimeOneEmpty)
{
    return "Please, insert a Date!";
}

This is following error

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index at System.Web.UI.ControlCollection.get_Item(Int32 index) at Lirex.WayBillModule.b__2f(TableRow row) at System.Linq.Enumerable.Any[TSource](IEnumerable1 source, Func2 predicate)

解决方案

You can try below if will work, it will check for empty text box on all visible row, just modify to check for the other control type.

        var tableRows = Table1.Rows.Cast<TableRow>().Where(row => row.Visible);

        bool hasEmptyField = false;
        foreach (var row in tableRows.Where(row => row.Cells.Cast<TableCell>()
            .SelectMany(
                item => item.Controls.Cast<Control>()
                    .Where(cntrl => cntrl.GetType() == typeof (TextBox)))
            .Any(cntrl => string.IsNullOrEmpty(((TextBox) cntrl).Text))))
        {
            hasEmptyField = true;
            break;
        }

        if (hasEmptyField)
        {
            //Do what you want...
        }

Edited the answer. Last query was only checking the last visible row. Made some changes and now using the WHERE clause instead of LAST in getting the visible rows.. Changes also include loop for each row.

Below is the original code I posted checking for the LAST visible row only.

        var tableRow = Table1.Rows.Cast<TableRow>().Last(row => row.Visible);
        var hasEmptyTextBox =
            tableRow.Cells.Cast<TableCell>()
                .SelectMany(
                    item => item.Controls.Cast<Control>()
                        .Where(cntrl => cntrl.GetType() == typeof(TextBox)))
                .Any(cntrl => string.IsNullOrEmpty(((TextBox)cntrl).Text));

这篇关于如何办理登机手续string.IsNullOrEmpty和IsDateEmpty所有可见单元格的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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