使用Devexpress,我的网格出现了一个奇怪且令人沮丧的问题。
我有以下网格:
我有一个主行(选定)行,我希望用户让它创建基于动态的详细行并填充它们。这都与后面的sql db相关联。
我的问题就像你在图片中看到的那样,当我按下'创建细节'按钮,创建2个新行而不是1.
我完全不知道为什么会这样(因为我的主addrow -function没有这种行为)。
我的代码中出错了什么?
public partial class Get_Recurring_Items : DevExpress.XtraBars.Ribbon.RibbonForm
{
int companyID;
DataSet ds;
SqlDataAdapter daRecurrentItems;
SqlDataAdapter daRecurrentItemsDetails;
public static SqlConnection conn;
public Get_Recurring_Items(int _companyID)
{
companyID = _companyID;
InitializeComponent();
DisplayData();
btn_CreateDetailRow.ItemClick += Btn_CreateDetailRow_ItemClick;
gridView.RowUpdated += GridView_RowUpdated;
btn_Save.ItemClick += Btn_Save_ItemClick;
gridView.InitNewRow += GridView_InitNewRow;
detailsView.InitNewRow += DetailsView_InitNewRow;
}
private DataSet GetMasterDetail()
{
ds = new DataSet();
ds.Clear();
ds.Tables.Add(CreateReccuringItemsTable());
ds.Tables.Add(CreateDetailsTable());
DataColumn keyColumn = ds.Tables["ReccuringItems"].Columns["ID"];
DataColumn foreignKeyColumn = ds.Tables["ReccuringItemsDetails"].Columns["Recurring_Item_ID"];
ds.Relations.Clear();
ds.Relations.Add("ItemsDetails", keyColumn, foreignKeyColumn, false);
gridControl.LevelTree.Nodes.Add("ItemsDetails", gridView);
return ds;
}
private DataTable CreateReccuringItemsTable()
{
conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
DataTable tbl = new DataTable("ReccuringItems");
daRecurrentItems = new SqlDataAdapter("SELECT DISTINCT * FROM Company_Recurring_Items WHERE companyID = @companyID", conn);
daRecurrentItems.SelectCommand.Parameters.Add(new SqlParameter
{
ParameterName = "@companyID",
Value = companyID,
SqlDbType = SqlDbType.Int,
});
daRecurrentItems.Fill(tbl);
return tbl;
}
private DataTable CreateDetailsTable()
{
conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
DataTable tbl = new DataTable("ReccuringItemsDetails");
daRecurrentItemsDetails = new SqlDataAdapter("SELECT DISTINCT details.ID, details.Recurring_Item_ID, details.Description , details.Creation_Date FROM Company_Recurring_Items_Details as details", conn);
daRecurrentItemsDetails.Fill(tbl);
return tbl;
}
private DataSet getReccuringItems()
{
ds = new DataSet();
ds.Clear();
ds.Tables.Add(CreateReccuringItemsTable());
return ds;
}
void DisplayData()
{
gridControl.DataSource = GetMasterDetail().Tables["ReccuringItems"];
gridView.Columns[0].VisibleIndex = -1;
gridView.Columns[1].VisibleIndex = -1;
MessageBox.Show("count = " + detailsView.Columns.Count.ToString());
if (gridView.Columns.Count > 0)
{
detailsView.Columns.Clear();
string[] fieldNames = new string[] { "ID", "Recurring_Item_ID", "Description", "Creation_Date" };
DevExpress.XtraGrid.Columns.GridColumn column;
detailsView.Columns.Clear();
for (int i = 0; i < fieldNames.Length; i++)
{
column = detailsView.Columns.AddField(fieldNames[i]);
column.VisibleIndex = i;
}
}
try
{
}
catch (Exception e3) { MessageBox.Show(e3.ToString()); }
//gridView.CellValueChanged += GridView_CellValueChanged;
}
private void DetailsView_InitNewRow(object sender, InitNewRowEventArgs e)
{
detailsView.SetRowCellValue(e.RowHandle, "Description", "Description");
detailsView.SetRowCellValue(e.RowHandle, "Creation_Date", DateTime.Now.ToString());
}
private void Btn_CreateDetailRow_ItemClick(object sender, ItemClickEventArgs e)
{
bool test = true;
GridView view = gridView.GetDetailView(gridView.FocusedRowHandle, 0) as GridView;
if (view != null && test == true)
{
view.AddNewRow();
test = false;
}else
{
MessageBox.Show("You must select a master row (item) to create a new detail!");
}
}
private void GridView_InitNewRow(object sender, InitNewRowEventArgs e)
{
gridView.SetRowCellValue(e.RowHandle, "companyID", companyID);
}
private void GridView_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
GridView currentView = sender as GridView;
currentView.UpdateCurrentRow();
// btn_Save.PerformClick();
if (ds.Tables["ReccuringItems"] != null)
{
//btn_Save.PerformClick();
}
else
{
MessageBox.Show("error");
}
gridView.RefreshData();
}
private void Btn_Save_ItemClick(object sender, ItemClickEventArgs e)
{
ColumnView View = (ColumnView)gridControl.FocusedView;
if (!(View.PostEditor() && View.UpdateCurrentRow())) return;
SqlCommandBuilder scb = new SqlCommandBuilder(daRecurrentItems);
SqlCommandBuilder scb2 = new SqlCommandBuilder(daRecurrentItemsDetails);
try
{
daRecurrentItems.AcceptChangesDuringUpdate = true;
daRecurrentItemsDetails.AcceptChangesDuringUpdate = true;
daRecurrentItems.Update(ds.Tables["ReccuringItems"]);
daRecurrentItemsDetails.Update(ds.Tables["ReccuringItemsDetails"]);
}
catch (SqlException ex)
{
Debug.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
}
我尝试使用以下解决方法(但就像我使用了计数器一样)但没有成功:
private void Btn_CreateDetailRow_ItemClick(object sender, ItemClickEventArgs e)
{
bool test = true;
GridView view = gridView.GetDetailView(gridView.FocusedRowHandle, 0) as GridView;
if (view != null && test == true)
{
view.AddNewRow();
test = false;
}else
{
MessageBox.Show("You must select a master row (item) to create a new detail!");
}
}
它仍然会在按钮上创建2个细节行...
答案 0 :(得分:0)
要解决此问题,您需要设置
GridOptionsView.NewItemRowPosition Property
为None,以便从详细信息网格中隐藏NewRow。
GridOptionsView.NewItemRowPosition = NewItemRowPosition.None;