2020年12月28日 星期一

C# DataTable 分頁處理

 #1

DataTable dt=GetDataTable();

Int pageSize=5000;

int count =dt.Rows.Count/pageSize;

for(int i=0;i<=count;i++)

{

        DataTable dtNew=dt.AsEnumerable().Skip(i * pageSize).Take(pageSize).CopyToDataTable();


}


#2

public DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)//PageIndex表示第幾頁,PageSize表示每頁的記錄數

  {

   if (PageIndex == 0)

    return dt;//0頁代表每頁數據,直接返回


   DataTable newdt = dt.Copy();

   newdt.Clear();//copy dt的框架


   int rowbegin = (PageIndex - 1) * PageSize;

   int rowend = PageIndex * PageSize;


   if (rowbegin >= dt.Rows.Count)

    return newdt;//源數據記錄數小於等於要顯示的記錄,直接返回dt


   if (rowend > dt.Rows.Count)

    rowend = dt.Rows.Count;

   for (int i = rowbegin; i <= rowend - 1; i++)

   {

    DataRow newdr = newdt.NewRow();

    DataRow dr = dt.Rows[i];

    foreach (DataColumn column in dt.Columns)

    {

     newdr[column.ColumnName] = dr[column.ColumnName];

    }

    newdt.Rows.Add(newdr);

   }

   return newdt;

  }


2020年12月14日 星期一

Get column index from datagridview cell on mousedown or mouse enter event

 Private clickedCell As DataGridViewCell


Private Sub dataGridView1_MouseDown(ByVal sender As Object, _

    ByVal e As MouseEventArgs) Handles dataGridView1.MouseDown


    ' If the user right-clicks a cell, store it for use by the 

    ' shortcut menu.

    If e.Button = MouseButtons.Right Then

        Dim hit As DataGridView.HitTestInfo = _

            dataGridView1.HitTest(e.X, e.Y)

        If hit.Type = DataGridViewHitTestType.Cell Then

            clickedCell = _

                dataGridView1.Rows(hit.RowIndex).Cells(hit.ColumnIndex)

        End If

    End If


End Sub

Jquery ajax載入等待執行結束再繼續執行下面程式碼操作

 Jquery ajax載入等待執行結束再繼續執行下面程式碼操作

--

不錯的方法

2020年8月21日 星期五

2020年7月29日 星期三

判断字符是否为中文

判断字符是否为中文

1、用ASCII码判断

在 ASCII码表中,英文的范围是0-127,而汉字则是大于127,具体代码如下:

代码如下:

string text = "是不是汉字,ABC,柯乐义";
       for (int i = 0; i < text.Length; i++)
       {
            if ((int)text[i] > 127)
                  Console.WriteLine("是汉字");
            else
                  Console.WriteLine("不是汉字");
       }
 

 

2、用汉字的 UNICODE 编码范围判断

汉字的UNICODE编码范围是4e00-9fbb,具体代码如下:

 代码如下:

string text = "是不是汉字,ABC,keleyi.com";
      char[] c = text.ToCharArray();
 

       for (int i = 0; i < c.Length;i++)
       if (c[i] >= 0x4e00 && c[i] <= 0x9fbb)
              Console.WriteLine("是汉字");
       else
              Console.WriteLine("不是汉字");

 

 

3、用正则表达式判断

用正则表达式判断也是用汉字的 UNICODE 编码范围,具体代码如下:

代码如下:

string text = "是不是汉字,ABC,keleyi.com";
        for (int i = 0; i < text.Length; i++)
        {
               if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]+{1}quot;))
                   Console.WriteLine("是汉字");
               else
                   Console.WriteLine("不是汉字");
        }

winform中如何取得 DataGridView 单元格的屏幕位置

winform中如何取得 DataGridView 单元格的屏幕位置
首先取得DataGridView的坐标位置:
int dgvX = dataGridView1.Location.X;
int dgvY = dataGridView1.Location.Y;
然后取得选中单元格的坐标在DataGridView中的坐标位置:
int cellX = dataGridView1.GetCellDisplayRectangle(ColumnIndex,RowIndex,false).X;
int cellY = dataGridView1.GetCellDisplayRectangle(ColumnIndex,RowIndex,false).Y;
最后可以得到每个单元格相对于form的坐标为:
int x = dgvX + cellX;
int y = dgvY + cellY;

//点击单元格获取单元格位置并显示控件A.png

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex > 4)
            {
                int dgvX = dataGridView1.Location.X;
                int dgvY = dataGridView1.Location.Y;
                int cellX = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).X;
                int cellY = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Y;
                int x = dgvX + cellX;
                int y = dgvY + cellY;
                dateTimePicker1.Location =  new Point(x,y);
                dateTimePicker1.Visible = true;
            }
        }