简体   繁体   English

以编程方式添加新行时调整gridview rowIndex

[英]Adjust gridview rowIndex when programmatically adding new row

I've seen this article http://asp.net-informations.com/gridview/newrow.htm and this post http://forums.asp.net/p/1534978/3725419.aspx#3725419 and I've done it for have that separator row collapsible with jquery and it's working great in display mode. 我已经看过这篇文章http://asp.net-informations.com/gridview/newrow.htm和这篇文章http://forums.asp.net/p/1534978/3725419.aspx#3725419 ,我已经完成了它的分隔符行可与jquery折叠,并且在显示模式下运行良好。 The problems occuring when try to do something else with gridview, because there's a weird behaviour.. I've add a simple button that have just to set a radiobutton that's in every gridview row (except in the new added GroupHeaders rows). 当尝试使用gridview进行其他操作时会出现问题,因为有一个怪异的行为。我添加了一个简单的按钮,该按钮只需要设置每个gridview行中的单选按钮(新添加的GroupHeaders行中除外)。 Then if I have added two new row he is skipping setting the last two rows in the GridView.. 然后,如果我添加了两行,他将跳过在GridView中设置最后两行。

    public void AddNewRow(object sender, GridViewRowEventArgs e)
    {
        GridView GridView1 = (GridView)sender;
        GridViewRow NewTotalRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
        TableCell HeaderCell = new TableCell();
        HeaderCell.Attributes.Add("onclick", "collapsible('" + rowgroup + "')");
        NewTotalRow.Cells.Add(HeaderCell);
        TableCell HeaderCellIndex = new TableCell();
        int indexCorrente = e.Row.RowIndex + index;
        HeaderCellIndex.Text = indexCorrente.ToString();
        NewTotalRow.Cells.Add(HeaderCellIndex);
        GridView1.Controls[0].Controls.Add(NewTotalRow);
    }

    protected void gdDettaglio_RowCreated(object sender, GridViewRowEventArgs e)
    {
        bool newRow = false;
        if ((DataBinder.Eval(e.Row.DataItem, "Stato") != null))
        {
            if (statoCorrente != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Stato").ToString()))
                newRow = true;
        }
        if (newRow)
        {
            AddNewRow(sender, e);
        }
    }

Printing the row index (just to check it) next to each row I'm displaying this situation (with two GroupHeader rows added): 在每行旁边打印行索引(仅供检查),我正在显示这种情况(添加了两个GroupHeader行):

(The index are as they are printing them in gdDettaglio_RowCreated for GroupHeadrs rows and on gdDettaglio_OnDataBound for the other rows) (索引是在GroupHeadrs行的gdDettaglio_RowCreated和其他行的gdDettaglio_OnDataBound上打印的)

-----------------------------------------
|           HEADER                      |
|---------------------------------------|
-----------------------------------------
|           Gruppo 1        |  index -1  |
|---------------------------------------|
|   grp1 | x | blablabla |  |  index 0  |
|   grp1 | y | blablabla |  |  index 1  |
|   grp1 | z | blablabla |  |  index 2  |
|   grp1 | x | blablabla |  |  index 3  |
|   grp1 | x | blablabla |  |  index 4  |
|---------------------------------------|
|           Gruppo 2        |  index -1  |
|---------------------------------------|
|   grp2 | x | blablabla |  |  index 5  |
|   grp2 | y | blablabla |  |  index 6  |
|   grp2 | z | blablabla |  |  index 7  |
|   grp2 | z | blablabla |  |  index 8  |
|   grp2 | z | blablabla |  |  index 9  |
|   grp2 | z | blablabla |  |  index 10 |
-----------------------------------------

in the button code I've just: 在按钮代码中,我只是:

foreach (GridViewRow riga in gdDettaglio.Rows)
{
    if (riga.RowType == DataControlRowType.DataRow)
    {
        RadioButtonList rad = (RadioButtonList)riga.FindControl("rad");
        rad.SelectedValue = "True";
    }
}

UPDATE: 更新:

Doing the same thing on jquery work, it affects all the row: 在jquery上做同样的事情,会影响到所有行:

    function accettaTutte() {
        $("#<%=gdDettaglio.ClientID%> tr:has(td)").each(function () {
            var items = $(this).find("[id$='radDaPa'] input:radio'");
            for (var i = 0; i < items.length; i++) {
                if (items[i].value == 'True') {
                    if (!(items[i].checked)) {
                        items[i].checked = true;
                    }
                    break;
                }
            }
        });
        return false;
    }

But I still need to do a foreach on that gridview, to update db, some idea on what could try to do? 但是我仍然需要在那个gridview上做一个foreach,以更新数据库,关于可以尝试做什么的一些想法? On every row I've also a "single row save" ImageButton, but clicking on it on the last two rows it's not firing the RowCommand event... It's like the two added GroupHeader rows are pushing out the last two data rows, no matter about the index.. If I click the ImageButton on the row with displayed (using Text='<%#Container.DataItemIndex%>') rowIndex 2, in the rowCommand it become rowIndex 3, but it modified the right row, the one I've clicked.. If i do the same on row 7, it become 9.. But if forced it to get value on rowIndex 11, U'm getting ArgumentOutOfRangeException, because Rows.Count It's still 11.. 在每一行上,我还具有一个“单行保存” ImageButton,但是在最后两行上单击它并不会触发RowCommand事件……这就像添加的两个GroupHeader行正在推出最后两个数据行,如果我单击显示的行上的ImageButton(使用Text ='<%#Container.DataItemIndex%>')rowIndex 2,则在rowCommand中它将变为rowIndex 3,但它修改了右行,如果我在第7行上执行相同操作,它将变为9。.但是如果强迫它在rowIndex 11上获取值,则U正在获取ArgumentOutOfRangeException,因为Rows.Count仍为11。

好的,现在我将GridView DataSource添加到列表中,其中的空元素与GroupHeaders行一样多,因此GridView.Rows.Count足以获取所有行。

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

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