DataGridView 常用功能記錄

DataGridView.DataSource = null;  //清空

DataGridView.DataSource =   //連結來源

DataGridView.ReadOnly = true; //設定成唯讀

DataGridView.Rows 取得行的記錄

DataGridView.Rows[index].Cells[index].Value;  取得Index的值

常常分不清Row、Column、Cell 差異,用圖說:


例如:使用者在關鍵字欄位輸入關鍵字,每個欄位都會被檢查是否包含,再根據關鍵子顯示結果
try{
    string searchValue = this.textBoxSearchValue.Text;
    this.dataGridViewProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    bool valueResult = false;
    foreach (DataGridViewRow row in this.dataGridViewProjects.Rows)//88
    {
      int intResult = this.dataGridViewProjects.Rows.Count;
      for (int i = 0; i < row.Cells.Count; i++)//3
      {
	if (row.Cells[i].Value != null && row.Cells[i].Value.ToString().Contains(searchValue))
	{
	    int rowIndex = row.Index;
	    this.dataGridViewProjects.Rows[rowIndex].Selected = true;
	    valueResult = true;
	        break;
	}
      }
    }
    if (valueResult)
    {
        this.dataGridViewProjects.DataSource = null;
        string projectSql = $"SELECT COP_NO, PROJM_NO,PROJM_NAME FROM [AC].[dbo].[Pjm] " +
		$"WHERE  COP_NO LIKE '%{searchValue}%' " +
		$"or PROJM_NO LIKE '%{searchValue}%' " +
		$"or PROJM_NAME LIKE '%{searchValue}%'";
        DataTable dtProject = DBHelper.GetDataTable(projectSql);
        this.dataGridViewProjects.DataSource = dtProject;
        this.dataGridViewProjects.ReadOnly = true;
    }

  }
catch (Exception exc)
{
    MessageBox.Show(exc.Message);
}

留言

這個網誌中的熱門文章

簽字筆 奇異筆 光碟筆 油漆筆 麥克筆 差別在哪

DC 常用指令

如何在 Excel 2010 中將數值設定成以「千」或「百萬」為單