如何在datagridview的下一页添加一行

时间:2013-12-27 10:48:13

标签: c# winforms datagridview

我有一个列表,其中包含一些与datagridview绑定的记录。此列表包含一些属性,如pageno,pagebreak,rowno等。现在我在Datagridview中添加一些新行,当网格位于第1页时,我添加的行插入在正确的位置,但当我移动到Datagridview的第二页或第三页并添加一个新行时,它插入datagridview的最顶部。我已经做了一些代码,用于在正确的位置插入行,我还附加了一个图像,以便更好地理解我的问题..

foreach (RowType Itemm in box9.SelectedItems)
{
    FRReportRow newrow = new FRReportRow();
    name = Itemm.TypeName;//box9.Text.ToString();

    var ro = FixedSpecial.Where(x => x.TypeName == name).SingleOrDefault();
    newrow.Item = name;
    newrow.RowType = ro.IndexType;
    if (newrow.RowType == 7)
        PageBreaker = 1;
    newrow.RowInfo = GetRowInfo(newrow.RowType);
    if (newrow.RowInfo == string.Empty)
    {
        newrow.RowInfo = M3.FWL.UI.LayoutUc.EnumRowInfo.F.ToString();
    }
    ListRow.Add(newrow);

    //To manage items added with page breaker
    if (PageBreaker == 1 && newrow.RowType != 7)
        AfterPageBreak.Add(newrow);
}
}

int prevpage;
List<FRReportRow> ListToBeAdded = new List<FRReportRow>();
if (IsValidationRequired)
ListToBeAdded = IsMultipleItemAlreadyExist(ListRow);
else
ListToBeAdded = ListRow;
if (ListToBeAdded.Count > 0)
{
    groupBox1.Enabled = true;
    var pagebreak = lstnewRow.Where(x => x.RowType == 7).ToList();
    ListToBeAdded.ForEach(x => x.Pageno = Convert.ToInt32(nudpage.Value));
    //Check Items after pagebreaker to change page no
    if (AfterPageBreak.Count > 0)
    {
        foreach (FRReportRow item in ListToBeAdded)
        {
            int count = AfterPageBreak.Where(x => x == item).Count();
            if (count > 0)
                item.Pageno = item.Pageno + 1;
        }
    }
    int rowNo = 0;
    if (!PageBreakExist)
        rowNo = gvlayoutload.SelectedRows[0].Index;//gvlayoutload.Rows.Count;
    else
        PageBreakExist = false;

    int AfterPageRowNo = 0;
    foreach (FRReportRow item in ListToBeAdded)
    {
#region Determine the Rowno of the Item
        int count = 0;
        if (AfterPageBreak.Count > 0)
        {
            count = AfterPageBreak.Where(x => x == item).Count();
            if (count > 0)
                AfterPageRowNo = AfterPageRowNo + 1;
        }
        if (count > 0) //When item added after pagebreak in one go.
            rowNo = AfterPageRowNo;
        else
            rowNo = rowNo + 1;
#endregion

        item.RowNo = rowNo;
        item.size = 8;
        item.AlignText = 0;
        item.AlignData = 2;
        item.Indent = 0.1;
        item.Loadas = 0;
        item.fNegativeFormat = 0;
        item.Format = 0;
        // lstnewRow.Add(item);
        mRowIndex = gvlayoutload.SelectedRows[0].Index;

        lstnewRow.Add(item);
        else if (mRowIndex >= 0 && mRowIndex < lstnewRow.Count) // other than first and last row
        {
            lstnewRow.Insert(mRowIndex + 1, item);
        }
        for (int i = 0; i < lstnewRow.Count; i++)
        {
            lstnewRow[i].RowNo = i + 1;
            //lstnewRow[i].isUpdated = true;
        }
    }

    if (AfterPageBreak.Count > 0)
    {
        pagebreak = lstnewRow.Where(x => x.RowType == 7).ToList();
        PageBreaker = 0;
    }
    var newlst = lstnewRow.ToList();
    this.fRReportRowBindingSource.DataSource = newlst;

enter image description here

0 个答案:

没有答案