0

I have a table and I would like to check if it populates or not.

Below I've shown how I build the grid and how it looks in the run time environment; how would I count the rows?

How I build the grid:

@using System.Dynamic
@{
    var result = new List<dynamic>();
    //Model.TrackingNumber = Model.RequestNumber.ToString();

    foreach (var proprow in Model.DDS)
    {
        var row = (IDictionary<string, object>)new ExpandoObject();
        Dictionary<string, object> eachPropRow = (Dictionary<string, object>)proprow;


        var fileName = proprow["Township"] + ", " + proprow["Erf"] + ", " + proprow["Portion"] + ", " + proprow["Property_Type"] + ", " + proprow["Unit"] + " " + "V?, " + DateTime.Now.ToString("(yyyy-MM-dd (HH.mm))"); //+ DateTime.Now.ToString("(yyyy-MM-dd (HH.mm))")
        foreach (KeyValuePair<string, object> keyValuePair in eachPropRow)
        {
            System.Web.HtmlString linkID;
            if (keyValuePair.Key == "Prop_ID")
            {

                linkID = Html.ActionLink(keyValuePair.Value.ToString(), "Valuation", new { PropID = keyValuePair.Value, Token = Model.Token, fileName, RequestNumber = Model.RequestNumber, ClientName = Model.ClientName, ReasonForValuation = Model.ReasonForValuation, ContactPerson = Model.ContactPerson, ContactNumber = Model.ContactNumber, Relation = Model.Relation, AccountNumber = Model.AccountNumber, BondAmount = Model.BondAmount, PurchasePrice = Model.PurchasePrice}, new { @class = "DownloadLink" });
                row.Add(keyValuePair.Key, linkID);
            }
            else
            {
                row.Add(keyValuePair);
            }
        }
        result.Add(row);
    }


    var grid = new WebGrid(result,null,null,30,false,false);

    @*@grid.GetHtml(tableStyle: "grid", headerStyle: "head", alternatingRowStyle: "alt");*@

    @grid.GetHtml(
       tableStyle: "grid",
       headerStyle: "grid-header",
       alternatingRowStyle: "grid-alternating-row", //grid-alternating-row
       selectedRowStyle: "grid-selected-row",
       rowStyle: "grid-row-style"

       )

  }

Below I've listed the table as its shown in run time console:

<table class="grid">
    <thead>
        <tr class="grid-header"><th scope="col">Prop_ID</th>
            <th scope="col">Erf</th>
            <th scope="col">Street</th>
            <th scope="col">Unit</th>
            <th scope="col">Portion</th>
            <th scope="col">Size</th>
            <th scope="col">Suburb</th>
            <th scope="col">Township</th>
            <th scope="col">Province</th>
            <th scope="col">Property_Type</th>
            <th scope="col">Owner</th>
            <th scope="col">Title_Deed_No</th>
            <th scope="col">Purch_Price</th>
            <th scope="col">Purch_Date</th>
        </tr>
    </thead>
    <tbody>
        <tr class="grid-row-style">
            <td><a class="DownloadLink" href="/ReturnProperties/Valuation?PropID=14351160&amp;Token=f7a9d22c-ce87-45df-a8b1-a05bdba9e068&amp;fileName=HIGHVELD%20EXT%207%2C%201402%2C%200%2C%20FH%2C%20%20V%3F%2C%20(2013-07-15%20(14.53))&amp;RequestNumber=45735864378386V1">14351160</a></td>
            <td>1402</td>
            <td>77 SANTA MONICA</td>
            <td></td>
            <td>0</td>
            <td>750</td>
            <td>CENTURION GOLF ESTATE</td>
            <td>HIGHVELD EXT 7</td>
            <td>GA</td>
            <td>FH</td>
            <td>MOSSOP MAUREEN</td>
            <td>T101413/2007</td>
            <td>4800000</td>
            <td>20070611</td>
        </tr>
        <tr class="grid-alternating-row">
            <td><a class="DownloadLink" href="/ReturnProperties/Valuation?PropID=14351160&amp;Token=f7a9d22c-ce87-45df-a8b1-a05bdba9e068&amp;fileName=HIGHVELD%20EXT%207%2C%201402%2C%200%2C%20FH%2C%20%20V%3F%2C%20(2013-07-15%20(14.53))&amp;RequestNumber=45735864378386V1">14351160</a></td>
            <td>1402</td>
            <td>77 SANTA MONICA</td>
            <td></td>
            <td>0</td>
            <td>750</td>
            <td>CENTURION GOLF ESTATE</td>
            <td>HIGHVELD EXT 7</td>
            <td>GA</td>
            <td>FH</td>
            <td>MOSSOP IAN PAUL</td>
            <td>T101413/2007</td>
            <td>4800000</td>
            <td>20070611</td>
        </tr>
    </tbody>
    </table>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pomster
  • 14,567
  • 55
  • 128
  • 204

2 Answers2

1

You could use the .NET XML libraries and XPath to determine that. Or use a free-online XPath testing tool and copy paste your XML into it and use the following XPath expression:

//table[@class='grid']//tr

Using the .NET 1.x apis, you could instantiate a new XML document and execute the XPath as follows:

XmlDocument tableDoc = new XmlDocument();
tableDoc.LoadXml( /* A string containing the HTML here -- but it better be well-formed */);
var trCount = tableDoc.SelectNodes("//table[@class='grid']//tr").Cast<XmlNode>().Count();

If you're using XHTML, you should use the below XPath so you don't have to worry about needing to create and use a NamespaceManager with SelectNodes(...):

//*[local-name()='table' and @class='grid']//*[local-name()='tr']

There's also the newer .NET API LINQ-to-XML. I'm not quite familiar enough with it to give you a good example.

HTH.

fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48
0

To get rowcount of webgrid-

 var grid = ISGetObject("WebGrid1");
 var getTotal = grid.TotalRows;
 var getChangesTotal = grid.RootTable.GetChangesCount();
 alert(getTotal + getChangesTotal);
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71