参考文章https://blog.csdn.net/lysc_forever/article/details/17093581?locationNum=14中多行显示问题,现在补充修改两个函数,将原来gdvAttribute_RowHeaderMouseClick函数中的代码剪切到dgvAttributesTable_MouseUp中,并补充一个dgvAttributesTable_RowStateChanged函数做选中状态检测,希望能帮到你。
首先添加全局标志变量
int iFlag=0;
然后
/// <summary>
/// 选中高亮显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <returns></returns>
private void dgvAttributesTable_MouseUp(object sender, MouseEventArgs e)
{
if (iFlag==1) {
IQueryFilter pQuery = new QueryFilterClass();
int count = this.dgvAttributesTable.SelectedRows.Count;
string val, col;
col = this.dgvAttributesTable.Columns[0].Name;
//当只选中一行时
if (count == 1)
{
val = this.dgvAttributesTable.SelectedRows[0].Cells[col].Value.ToString();
//设置高亮要素的查询条件
pQuery.WhereClause = col + "=" + val;
}
else//选中多行
{
int i;
string str;
for (i = 0; i < count - 1; i++)
{
val = this.dgvAttributesTable.SelectedRows[i].Cells[col].Value.ToString();
str = col + "=" + val + " OR ";
pQuery.WhereClause += str;
}
//添加最后一个要素的条件
val = this.dgvAttributesTable.SelectedRows[i].Cells[col].Value.ToString();
str = col + "=" + val;
pQuery.WhereClause += str;
}
IFeatureLayer pFLayer = mLayer as IFeatureLayer;
IFeatureSelection pFeatSelection;
pFeatSelection = pFLayer as IFeatureSelection;
pFeatSelection.SelectFeatures(pQuery, esriSelectionResultEnum.esriSelectionResultNew, false);
IViewRefresh viewRefresh = axMap.Map as IViewRefresh;
viewRefresh.ProgressiveDrawing = true;
viewRefresh.RefreshItem(mLayer);
iFlag = 0;
}
}
private void dgvAttributesTable_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
iFlag = 1;
}