GridView的新增与删除

1、注意点

  • 绑定的数据源只能是BindingList或者DataTable时,GridView.AddNewRow才能有效;不能绑定List,否则新增是无效的;即使设置了 gc.EmbeddedNavigator.Buttons.Append.Visible = true; 绑定List的按钮也是不可用的状态;
  • 保存另需实现
  • 绑定的字段必须和sql语句查询出的值一样

2、代码

/// <summary>
/// 新增
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void simpleButton2_Click(object sender, EventArgs e)
{
	if (string.IsNullOrEmpty(txtCode.Text.Trim()))
	{
		DevExpress.XtraEditors.XtraMessageBox.Show("模板编码不能为空!", "提示");
		return;
	}
	if (string.IsNullOrEmpty(txtName.Text.Trim()))
	{
		DevExpress.XtraEditors.XtraMessageBox.Show("模板名称不能为空!", "提示");
		return;
	}
	gridView2.AddNewRow();
	// 新增行获取焦点
	gridView2.FocusedRowHandle = gridView2.RowCount - 1;            
	// 使新增的行自动开启编辑模式,既所在焦点单元格显示闪动的光标
	gridView2.ShowEditorByMouse();
	gridView2.SetRowCellValue(gridView2.RowCount - 1, "VisibleIndex", gridView2.RowCount);//列初始值
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void simpleButton1_Click(object sender, EventArgs e)
{
	gridView2.DeleteSelectedRows();
}

/// <summary>
/// 行单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_Click(object sender, EventArgs e)
{
	int rowHandle = gridView1.FocusedRowHandle;
	if (rowHandle < 0) return;
	DataRow dr = gridView1.GetDataRow(rowHandle);
	if (Columns.Count > 0 )
	{
		Columns.Clear();
	}

	txtCode.Text = dr["CODE"].ToString();
	txtName.Text = dr["NAME"].ToString();
	txtSql.Text = dr["SQL"].ToString();
	string jsonString = dr["JSON"].ToString();
	Model = JsonConvert.DeserializeObject<GridViewModel>(jsonString);
	if (Model != null)
	{
		radioGroup1.Text = Model.ViewType == 1 ? "表格" :"报表";
		checkEdit14.Checked = Model.Status == 1 ? true: false;
		checkEdit1.Checked = Model.ShowGroupPanel;
		textEdit2.Text = Model.GroupPanelText;

		checkEdit2.Checked = Model.AllowCellMerge  ;
		checkEdit3.Checked = Model.ShowAutoFilterRow  ;
		checkEdit4.Checked = Model.RowAutoHeight  ;
		checkEdit5.Checked = Model.AllowColumnMoving  ;

		checkEdit6.Checked = Model.AllowSort  ;
		checkEdit9.Checked = Model.EnableColumnMenu  ;
		checkEdit7.Checked = Model.ColumnAutoWidth  ;
		checkEdit8.Checked = Model.Editable  ;

		checkEdit10.Checked = Model.EnableAppearanceOddRow  ;
		checkEdit12.Checked = Model.EnableAppearanceEvenRow  ; // 偶数行
		checkEdit13.Checked = Model.ShowFooter  ;
		checkEdit11.Text = Model.IndicatorWidth.ToString();
		Columns = Model.Columns;
		gridControl2.DataSource = Columns;
		// 初始化
		InitGridControl(gridControl2, true);
	}
}

3、绑定下拉框

  • 定义一个实体类
public class ComboxList
{
	public string ItemCode { get; set; }
	public string ItemName { get; set; }

	public ComboxList(string itemCode, string itemName)
	{
		ItemCode = itemCode;
		ItemName = itemName;
	}
}
  • 绑定,但一定要在设置DataSource之前,Columns后面的索引要和绑定的FiledName一致
BindingList<ComboxList> blType = new BindingList<ComboxList>() {
	new ComboxList("0","表格"),
	new ComboxList("1","报表")
	};

RepositoryItemLookUpEdit riluType = new RepositoryItemLookUpEdit()
{
	DataSource = blType,
	DisplayMember = "ItemName",
	ValueMember = "ItemCode",
	NullText = ""
	};
// 后面的30和70可以视作百分比,因为是自动宽度
riluType.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
                new DevExpress.XtraEditors.Controls.LookUpColumnInfo("ItemCode","代码",30),
                new DevExpress.XtraEditors.Controls.LookUpColumnInfo("ItemName","名称",70),
            });	
gridView1.Columns["TYPE"].ColumnEdit = riluType;
posted @ 2022-08-20 15:26  his365  阅读(312)  评论(0编辑  收藏  举报