Ejemplo n.º 1
0
        /// <summary>
        /// 粘贴表格
        /// </summary>
        /// <param name="dgv">表格控件</param>
        /// <param name="LsGDCol">固定表格列</param>
        public static void PasteToDGV(DataGridView dgv, List <int> LsGDCol, ref string sMsg)
        {
            try
            {
                int[] iRC = CABCDGV.GetSelMinRCMaxRC(dgv);
                if (iRC[0] < 0 || iRC[1] < 0)
                {
                    return;
                }
                //获取剪贴板内容
                string pasteText = Clipboard.GetText();
                //判断是否有字符存在
                if (string.IsNullOrEmpty(pasteText))
                {
                    return;
                }
                //以换行符分割的数组
                pasteText = pasteText.Trim().Replace("\r\n", "\n");
                string[] lines = pasteText.Trim().Split('\n');
                List <List <string> > lsPaste = new List <List <string> >();

                for (int j = 0; j < lines.Length; j++)
                {
                    string[] vals = lines[j].Split('\t');
                    lsPaste.Add(vals.ToList());
                }
                foreach (DataGridViewCell nCell in dgv.SelectedCells)
                {
                    if (LsGDCol != null && LsGDCol.Contains(nCell.ColumnIndex))
                    {
                        continue;
                    }

                    int iR = nCell.RowIndex - iRC[0];
                    int iC = nCell.ColumnIndex - iRC[1];
                    if (iR < lsPaste.Count)
                    {
                        if (iC < lsPaste[iR].Count)
                        {
                            nCell.Value = lsPaste[iR][iC];
                        }
                    }
                }
            }
            catch (Exception MyEx)
            {
                sMsg = "粘贴出错:" + MyEx.Message;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 按规律设定表格单元格
        /// </summary>
        /// <param name="dGV">表格名称</param>
        /// <param name="iCRC">循环行数</param>
        /// <param name="bAdd">是否递增</param>
        public static bool dGVSetByCrc(DataGridView dGV, int iCRC, bool bAdd)
        {
            try
            {
                List <int> LsRow = CABCDGV.GetSelRow(dGV);
                List <int> LsCol = CABCDGV.GetSelCol(dGV);
                if (LsRow.Count < iCRC + 1)
                {
                    return(false);
                }
                for (int n = 0; n < iCRC; n++)
                {
                    for (int i = 0; i < LsCol.Count; i++)
                    {
                        string sHead = dGV.Columns[LsCol[i]].HeaderText;
                        string str0  = "";
                        if (dGV.Rows[LsRow[n]].Cells[LsCol[i]].Value != null)
                        {
                            str0 = dGV.Rows[LsRow[n]].Cells[LsCol[i]].Value.ToString();
                        }
                        string str1 = "";
                        if (bAdd && dGV.Rows[LsRow[n + iCRC]].Cells[LsCol[i]].Value != null)
                        {
                            str1 = dGV.Rows[LsRow[n + iCRC]].Cells[LsCol[i]].Value.ToString();
                        }
                        else
                        {
                            str1 = str0;
                        }
                        double dStart = 0, dAdd = 0;
                        int    iStart = 0, iLen = 0;
                        string sChar = "";
                        int    iRes  = CABCSTR.Get2StrChange(str0, str1, ref dStart, ref dAdd, ref sChar, ref iStart, ref iLen);
                        if (iRes == 0)
                        {
                            return(false);
                        }
                        int kStart = 1;
                        if (bAdd)
                        {
                            kStart = 2;
                        }
                        for (int k = kStart; k < Math.Ceiling(((LsRow.Count / (double)iCRC))); k++)
                        {
                            if (n + k * iCRC >= LsRow.Count)
                            {
                                continue;
                            }
                            double dVal   = dStart + dAdd * k;
                            string sValue = "";
                            if (iRes == 1)
                            {
                                sValue = dVal.ToString();
                            }
                            else if (iRes == 2)
                            {
                                string sVal = new String('0', iLen) + dVal.ToString();
                                sValue = sChar.Insert(iStart, sVal.Substring(sVal.Length - iLen, iLen));
                            }
                            else if (iRes == 3)
                            {
                                sValue = str0;
                            }

                            dGV.Rows[LsRow[n + k * iCRC]].Cells[LsCol[i]].Value = sValue;
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }