1、新增一行:如果數據源沒有數據,就要添加一個新行
#region
if (this.gvDetail.RowCount > 0)
{
this.gvDetail.UpdateCurrentRow();
}
DataTable dt = (DataTable)this.gridDetail.DataSource;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("DETAILID");
dt.Columns.Add("ROUTEORDER");
dt.Columns.Add("PRODUCTID");
dt.Columns.Add("PRODUCTNAME");
dt.Columns.Add("SUBPRODUCTDEMANDDATE");
dt.Columns.Add("SUBPRODUCTDEMANDDATENAME");
dt.Columns.Add("CREATETIME");
dt.Columns.Add("ISADD");
}
DataRow dr = dt.NewRow();
dr["CREATETIME"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm");//新增日期
dr["ISADD"] = "Y";//新增行的標誌
dt.Rows.Add(dr);
dtSubProductDataSource = dt.Copy();
//dtSubProductBackUp = dt.Copy();
this.gridDetail.DataSource = dt;
this.gvDetail.RefreshData();
this.gvDetail.FocusedRowHandle = this.gvDetail.RowCount - 1;
this.gvDetail.FocusedColumn = this.gridColRouteOrder; //設置焦點列
#endregion
2、刪除一行:
if (this.gvDetail.GetSelectedRows().Length == 0)
{
MessageUtil.ShowTips("請選擇一條需要刪除的數據!");
return;
}
DialogResult dialog = MessageUtil.ShowOKCancelTips("是否刪除當前行?");
if (dialog == DialogResult.Cancel) { return; }
//獲取是否新增的標誌值
string isAdd = CommonUtils.ObjectToString(this.gvDetail.GetRowCellValue(this.gvDetail.GetSelectedRows()[0], "ISADD"));
if ("Y".Equals(isAdd))
{
//在界面新增的數據直接刪除
this.gvDetail.DeleteRow(this.gvDetail.GetSelectedRows()[0]);
this.gvDetail.UpdateCurrentRow();
}
else
{
//從後臺取出來的數據,刪除時先保存在proGroupDetailDeletedModel ,點擊保存以後再從數據庫刪除
string detialId = this.gvDetail.GetRowCellValue(this.gvDetail.GetSelectedRows()[0], "DETAILID").ToString(); //
ProductGroupDetail proDetial = new ProductGroupDetail();
proDetial.DetailID = Int32.Parse(detialId);
proGroupDetailDeletedModel.Add(proDetial);
//刪除數據源中的數據
this.gvDetail.DeleteRow(this.gvDetail.GetSelectedRows()[0]);
this.gvDetail.UpdateCurrentRow();
dtSubProductDataSource.AcceptChanges(); //刷新數據源,表格的數據刪除後,數據源會自動刷新
}
3、循環遍歷所有行,獲取表格的值:
private bool JudeDetailData(out List<ProductGroupDetail> detailList)
{
#region
bool result = false;
string message = "";
detailList = new List<ProductGroupDetail>();//返回的列表
if (this.gvDetail.RowCount <= 0)
{
MessageUtil.ShowTips("沒有添加!");
return false;
}
try
{
int rowCount = this.gvDetail.RowCount;
string routeOrderList = ",";//存放所有的,用於判斷是否重複
string subProductIdList = ",";//存放所有的,用於判斷是否重複
for (int i = 0; i < rowCount; i++)
{
ProductGroupDetail detail = new ProductGroupDetail();
DataRow dr = this.gvDetail.GetDataRow(i);
string isadd = dr["ISADD"].ToString();
detail.IsAdd = isadd;
//獲取
if (!"Y".Equals(isadd))
{
detail.DetailID = Int32.Parse(dr["DETAILID"].ToString());
}
//
string routeOrder = dr["ROUTEORDER"].ToString();
if (routeOrder.Length <= 0)
{
message += "第" + (i+1).ToString() + "行的不能爲空";
this.gvDetail.FocusedRowHandle = i;
this.gvDetail.FocusedColumn = this.gvDetail.Columns[""];
break;
}
//判斷是否重複
if (routeOrderList.IndexOf("," + routeOrder + ",") > -1)
{
message += "第" + (i + 1).ToString() + "行的不能重複";
this.gvDetail.FocusedRowHandle = i;
break;
}
detail.RouteOrder = Int32.Parse(routeOrder);
routeOrderList += routeOrder + ",";
//
string subProductId = dr["PRODUCTID"].ToString();
string subProductName = dr["PRODUCTNAME"].ToString();
if (subProductId.Length <= 0 || subProductName.Length<=0)
{
message += "第" + (i + 1).ToString() + "行不能爲空";
this.gvDetail.FocusedRowHandle = i;
break;
}
//判斷是否重複
if (subProductIdList.IndexOf("," + subProductId + ",") > -1)
{
message += "第" + (i + 1).ToString() + "行的不能重複";
this.gvDetail.FocusedRowHandle = i;
break;
}
detail.SubProductID = Int32.Parse(subProductId);
subProductIdList += subProductId + ",";
//
string subMandDate = dr["SUBPRODUCTDEMANDDATE"].ToString();
if (subMandDate.Length <= 0)
{
message += "第" + (i + 1).ToString() + "行的不能爲空";
this.gvDetail.FocusedRowHandle = i;
break;
}
detail.SubProductDemandDate = Int32.Parse(subMandDate);
detailList.Add(detail);
}
}
catch (Exception ex)
{
message = "獲取異常:" + ex.Message;
}
//返回值
if (message.Length > 0)
{
MessageUtil.ShowTips(message);
result = false;
}
else
{
result = true;
}
return result;
#endregion
}
4、行編輯的下拉框:要實現的方法
//表格編輯下拉框,在列SUBPRODUCTDEMANDDATE 綁定key值
private void cbDemandDate_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBoxEdit comboBoxEdit = (ComboBoxEdit)sender;
ComboxInfo comboxInfo = (ComboxInfo)comboBoxEdit.SelectedItem;
this.gvDetail.SetFocusedRowCellValue(this.gvDetail.Columns["SUBPRODUCTDEMANDDATE"], comboxInfo.Code);
}
//表格編輯下拉框,在列PRODUCTID 綁定id值
private void cbSubProductName_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBoxEdit comboBoxEdit = (ComboBoxEdit)sender;
ComboxInfo comboxInfo = null;
if (comboBoxEdit.SelectedItem != null && comboBoxEdit.SelectedItem.ToString().Length > 0)
{
comboxInfo = (ComboxInfo)comboBoxEdit.SelectedItem;
this.gvDetail.SetFocusedRowCellValue(this.gvDetail.Columns["PRODUCTID"], comboxInfo.Code);
this.gvDetail.SetFocusedRowCellValue(this.gvDetail.Columns["PRODUCTNAME"], comboxInfo.Name);
}
else
{
MessageUtil.ShowTips("輸入的值不存在!");
this.gvDetail.SetFocusedRowCellValue(this.gvDetail.Columns["PRODUCTID"], "");
}
}
//表格中使用下拉框控件時必須要實現的事件使用
private void repositoryItemComboBox_ParseEditValue(object sender, ConvertEditValueEventArgs e)
{
try
{
e.Value = CommonUtils.ObjectToString(e.Value);
e.Handled = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
GridControl光標定位到某一個單元格:
this.gridView1.FocusedRowHandle = 1;//行標
this.gridView1.FocusedColumn = this.dictCode;//某一列
this.gridView1.ShowEditor();//打開行編輯
GridControl添加按鈕列:
把列的ColumnEdit屬性設置爲RepositoryItemButtonEdit
把TextEditStyle屬性設置爲HideTextEditor;
把Buttons的Kind屬性設置爲Glyph;
把Button的Caption用於設置文字
把Buttons的TextOption的Appearance的HAlignment屬性設置爲Near;
ButtonStyle設置爲Office2003;