简体   繁体   English

绑定到中继器之前如何编辑数据集中的数据

[英]How to edit the data in dataset before binding to repeater

[context] c# / asp.NET [上下文] c#/ asp.NET

I have a table in a dataset from query which contains the ranking and the number of stars. 我在查询的数据集中有一个表,其中包含排名和星数。 I am trying to alter the ranking so that it correctly represents the ties. 我正在尝试更改排名,以便它正确地代表联系。 If five students achieved, 10, 7, 7, 7, 3 stars each, then the rank should be 1, 2, 2, 2, 5, rather than 1, 2, 3, 4, 5. 如果获得五个学生,每个学生分别获得10、7、7、7、3星,那么排名应该是1、2、2、2、5,而不是1、2、3、4、5。

The data in table has simply increasing rank in "RowNum" column and I am trying to modify the column data so that it represents ties correctly. 表中的数据仅在“ RowNum”列中具有递增的排名,我正在尝试修改列数据,以使其正确表示联系。

[code with simple ranking - code behind] [具有简单排名的代码-后面的代码]

if (ds.Tables[0].Rows.Count > 0)
{
    rpt_BoardList.DataSource = ds.Tables[0];
    rpt_BoardList.DataBind();
}

[aspx] [aspx]

<asp:Repeater ID="rpt_BoardList" runat="server">                    
<ItemTemplate>
    <tr>
        <td class="center"><%# Eval("RowNum") %></td>
        <td class="center"><%# Eval("username") %></td>
        <td class="center"><%# Eval("starCnt") %></td>
    </tr>
</ItemTemplate> 

This works fine. 这很好。

[NEW code with ties] [有关联的新代码]

Here is my attempt to modify the data in the table, but not working.... The table is sorted in descending order of number of stars. 这是我尝试修改表中的数据,但不起作用的方法。...表以星号的降序排列。 The logic here is remembering the last star counts and number of ties to modify the rank number. 这里的逻辑是记住最后的星星数和平局数以修改等级数。 I believe I am having trouble editing the table data. 我相信我在编辑表数据时遇到麻烦。

if (ds.Tables[0].Rows.Count > 0)
{
DataTable dTable = new DataTable();
dTable = ds.Tables[0].Copy();

for (int i = 0; i < dTable.Rows.Count; i++)
{
    string strRowNum = dTable.Rows[i]["RowNum"].ToString();
    string strStarCtn = dTable.Rows[i]["starCtn"].ToString();
    int temp = 0;

    if (last_starCtn == int.Parse(strStarCtn))
    {
        tie_counter++;
        temp = int.Parse(strRowNum) - tie_counter;
        dTable.Rows[i]["RowNum"] = temp.ToString();
    }
    else
    {
        tie_counter = 0;
        last_starCtn = int.Parse(strStarCtn);
    }

}

rpt_BoardList.DataSource = dTable;
rpt_BoardList.DataBind();

============================ ===========================

This currently returns an error. 当前这将返回一个错误。 And no data is shown on the aspx page. 而且,aspx页面上没有显示任何数据。

Here is the code 这是代码

for (int i = 0; i < dTable.Rows.Count; i++)
{
   string strRowNum = dTable.Rows[i][8].ToString(); // RowNum
   string strStarCtn = dTable.Rows[i][6].ToString();  // starCtn
   int temp = 0;
   int starCount;
   int numRows;

   bool result1 = int.TryParse(strRowNum, out numRows);
   bool result2 = int.TryParse(strStarCtn, out starCount);

   if (result1 && result2)
   {
       if (last_starCtn == starCount)
       {
           tie_counter++;
           temp = numRows - tie_counter;
           dTable.Rows[i][8] = temp.ToString();  // RowNum
       }
       else
       {
           tie_counter = 0;
           last_starCtn = starCount;
       }
   }
}

rpt_BoardList.DataSource = dTable;
rpt_BoardList.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM