Exemplo n.º 1
0
        /// <summary>
        /// DataGridView中的添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddButtonCallBack(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridView grid = (DataGridView)sender;
            DataTable    ta   = spellList.DataSource as DataTable;

            if (ta.Rows.Count > 0 && e.ColumnIndex >= 0 && e.RowIndex >= 0 && ta.Rows.Count > e.RowIndex)
            {
                if (grid.Columns[e.ColumnIndex].HeaderText.Equals(AddButtonHaderText))
                {
                    DataRow _row = null;
                    //是空的,直接新建一行
                    if (ta.Rows.Count <= 0)
                    {
                        _row = DataTableSerializer.FillDefaultNewRow(ref ta);
                    }
                    //非空复制上一行
                    else
                    {
                        _row = DataTableSerializer.CopyRowAt(ref ta, e.RowIndex);
                    }
                    m_IDIDAllocator.AcceptAlloc();
                    ta.Rows.InsertAt(_row, e.RowIndex + 1);
                    int spellID = (int)_row[0];

                    if (ta != gSpell_All_DataList)
                    {
                        //对全局数据进行排序,更新
                        DeleteTableEvent(gSpell_All_DataList);

                        SortDataTableByASC(ref gSpell_All_DataList);

                        AddTableEvent(gSpell_All_DataList);
                    }
                    DvideDataTableWithVocation();
                    SortDataTableByASC(ref ta);

                    DataRow myrow       = ta.Select("nID=" + spellID)[0];
                    int     newRowIndex = ta.Rows.IndexOf(myrow);
                    //默认选中第一行
                    this.spellList[0, 0].Selected = false;
                    if (this.spellList.Rows.Count > 1)
                    {
                        this.spellList.Rows[newRowIndex].Selected = true;
                    }
                    else
                    {
                        this.spellList.Rows[0].Selected = true;
                    }
                    this.spellList.FirstDisplayedScrollingRowIndex = newRowIndex - 1;
                }
            }
        }