功能:

       删除选中行

前台调用:

 string str="";
GridDelete(gv, "chk", out str);
MessageBox.Show(str);

底层类:

 /// <summary>
/// 删除所有选中行
/// </summary>
/// <param name="gv">GirdView 名称</param>
/// <param name="chkFieldName">复选框列名</param>
/// <param name="ErrorInfo">输出信息</param>
/// <returns>是否删除成功 成功=true 失败=false</returns>
public static bool GridDelete(DevExpress.XtraGrid.Views.Grid.GridView gv, string chkFieldName,out string ErrorInfo) {
ErrorInfo = "";
try
{
if (gv.RowCount < 1)
{
ErrorInfo = "列表无数据,删除失败!";
return false;
}
//gv 是否可以多选
bool isSelectMulti = gv.OptionsSelection.MultiSelect;
if (!isSelectMulti)
{
gv.OptionsSelection.MultiSelect = !isSelectMulti;
}
//用户是否选中了行
bool isRecord = false;
//遍历 让选中行的设置为选中状态
for (int i = 0; i < gv.RowCount; i++)
{
if (!string.IsNullOrEmpty(gv.GetRowCellValue(i, chkFieldName).ToString()))
{
if (Convert.ToBoolean(gv.GetRowCellValue(i, chkFieldName).ToString()))
{
gv.SelectRow(i);
isRecord = true;
}
}
}
//删除选中状态的行
gv.DeleteSelectedRows();
//恢复gv之前的状态
gv.OptionsSelection.MultiSelect = isSelectMulti; if (isRecord)
{
ErrorInfo = "删除成功!";
return true;
}
else {
ErrorInfo = "请选择你要删除的行!";
return false;
}
}
catch(Exception ex) {
ErrorInfo = ex.Message;
return false;
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

05-01 23:27