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);
}

留言