I have a radgrid where each row is basically a set of drop down boxes and text boxes that the user can edit...they can "Add" new rows of the same control and it should just add a defaulted row of that those controls in edit mode to the bottom of the grid...
When I first bind the Datasource it works fine...binds the one row that exists in the table to the grid..when I add a new row my routine that constructs a new datatable based on the existing rows in the grid and adding a new default row to the bottom works fine...however when it gets rebound to the grid the new row is not in edit mode and the original row is now doubled... (please see attached document for image samples...)
<Telerik:RadGrid ID="rgRuleCompares" runat="server"
onitemdatabound="rgRuleCompares_ItemDataBound"
EnableLinqExpressions="False" Width="100%" AllowPaging="True"
PageSize="10" DataMember="reg_rule_compares" GridLines="None"
onprerender="rgRuleCompares_PreRender1"
onitemcommand="rgRuleCompares_ItemCommand"
onneeddatasource="rgRuleCompares_NeedDataSource1" EnableViewState="true" >
<MasterTableView DataKeyNames="column_name,comparison_type,comparison_value,start_and_or" CommandItemDisplay="Top" AutoGenerateColumns="False" HeaderStyle-Height="10px" EditMode="InPlace" DataMember="reg_rule_compares" HeaderStyle-CssClass="gridHeader" AllowAutomaticDeletes="False" AllowAutomaticInserts="False" AllowAutomaticUpdates="False" >
<Columns>
<Telerik:GridDropDownColumn UniqueName="start_and_or" DataField="start_and_or" HeaderText="Comparison Item" ListDataMember="start_and_or" ListTextField="start_and_or" ListValueField="start_and_or"/>
<Telerik:GridDropDownColumn UniqueName="column_name" DataField="column_name" HeaderText="Register Column" ListDataMember="column_names" ListTextField="column_name" ListValueField="column_name"/>
<Telerik:GridDropDownColumn UniqueName="comparison_type" DataField="comparison_type" HeaderText="Comparison Type" ListDataMember="comparison_type" ListTextField="comparison_type" ListValueField="comparison_type"/>
<Telerik:GridBoundColumn UniqueName="comparison_value" DataField="comparison_value" DataType="System.String" HeaderText="Comparison Value" />
</Columns>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="True" />
</ClientSettings>
</Telerik:RadGrid>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = GetEmptyCompareTable();
dt = AddFirstCompareRow(dt);
GetCompareData(dt);
rgRuleCompares.DataBind();
}
}
protected void rgRuleCompares_ItemCommand(object sender, GridCommandEventArgs e)
{
DataTable dt = BuildCompareTableFromGrid();
dt = AddNewCompareRow(dt);
GetCompareData(dt);
}
private DataTable BuildCompareTableFromGrid()
{
int cnt = rgRuleCompares.Items.Count;
DataTable dt = GetEmptyCompareTable();
for (int i = 0; i < cnt; i++)
{
GridEditableItem editedItem = (GridEditableItem)rgRuleCompares.EditItems[0];
string val1 = editedItem.OwnerTableView.DataKeyValues[i]["start_and_or"].ToString();
string val2 = editedItem.OwnerTableView.DataKeyValues[i]["comparison_type"].ToString();
string val3 = editedItem.OwnerTableView.DataKeyValues[i]["comparison_value"].ToString();
string val4 = editedItem.OwnerTableView.DataKeyValues[i]["column_name"].ToString();
DataRow row;
row = dt.NewRow();
row["start_and_or"] = val1;
row["column_name"] = val4;
row["comparison_type"] = val2;
row["comparison_value"] = val3;
dt.Rows.Add(row);
}
return dt;
}
private DataTable GetEmptyCompareTable()
{
DataTable dt = new DataTable();
DataColumn col;
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "start_and_or";
dt.Columns.Add(col);
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "column_name";
dt.Columns.Add(col);
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "comparison_type";
dt.Columns.Add(col);
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "comparison_value";
dt.Columns.Add(col);
dt.TableName = "reg_rule_compares";
return dt;
}
private void GetCompareData(DataTable dt)
{
DataSet ds = new DataSet();
dt.TableName = "reg_rule_compares";
ds.Tables.Add(dt);
ds = BuildCompareDropDownTables(ds);
rgRuleCompares.DataSource = ds;
}
private DataSet BuildCompareDropDownTables(DataSet ds)
{
DataTable dt = new DataTable();
DataColumn col;
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "comparison_type";
dt.Columns.Add(col);
string[] s = { "Contains", "StartsWith", "EndsWith", "=" };
foreach (string str in s)
{
string field = str.ToString();
DataRow row;
row = dt.NewRow();
row["comparison_type"] = field;
dt.Rows.Add(row);
}
dt.TableName = "comparison_type";
ds.Tables.Add(dt);
dt = new DataTable();
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "column_name";
dt.Columns.Add(col);
string[] s1 = { "Memo", "Payee" };
foreach (string str in s1)
{
string field = str.ToString();
DataRow row;
row = dt.NewRow();
row["column_name"] = field;
dt.Rows.Add(row);
}
dt.TableName = "column_names";
ds.Tables.Add(dt);
dt = new DataTable();
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "start_and_or";
dt.Columns.Add(col);
string[] s2 = { "START", "AND", "OR" };
foreach (string str in s2)
{
string field = str.ToString();
DataRow row;
row = dt.NewRow();
row["start_and_or"] = field;
dt.Rows.Add(row);
}
dt.TableName = "start_and_or";
ds.Tables.Add(dt);
return ds;
}
private DataTable AddFirstCompareRow(DataTable dt)
{
DataRow row;
row = dt.NewRow();
row["start_and_or"] = "START";
row["column_name"] = "Memo";
row["comparison_type"] = "Contains";
row["comparison_value"] = "";
dt.Rows.Add(row);
return dt;
}
private DataTable AddNewCompareRow(DataTable dt)
{
DataRow row;
row = dt.NewRow();
row["start_and_or"] = "AND";
row["column_name"] = "Memo";
row["comparison_type"] = "Contains";
row["comparison_value"] = "";
dt.Rows.Add(row);
return dt;
}
private void rgRuleCompares_PreRender(object sender, System.EventArgs e)
{
foreach (GridItem item in rgRuleCompares.MasterTableView.Items)
{
if (item is GridEditableItem)
{
GridEditableItem editableItem = item as GridDataItem;
editableItem.Edit = true;
}
}
rgRuleCompares.Rebind();
}
protected void rgRuleCompares_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
}
When I first bind the Datasource it works fine...binds the one row that exists in the table to the grid..when I add a new row my routine that constructs a new datatable based on the existing rows in the grid and adding a new default row to the bottom works fine...however when it gets rebound to the grid the new row is not in edit mode and the original row is now doubled... (please see attached document for image samples...)
<Telerik:RadGrid ID="rgRuleCompares" runat="server"
onitemdatabound="rgRuleCompares_ItemDataBound"
EnableLinqExpressions="False" Width="100%" AllowPaging="True"
PageSize="10" DataMember="reg_rule_compares" GridLines="None"
onprerender="rgRuleCompares_PreRender1"
onitemcommand="rgRuleCompares_ItemCommand"
onneeddatasource="rgRuleCompares_NeedDataSource1" EnableViewState="true" >
<MasterTableView DataKeyNames="column_name,comparison_type,comparison_value,start_and_or" CommandItemDisplay="Top" AutoGenerateColumns="False" HeaderStyle-Height="10px" EditMode="InPlace" DataMember="reg_rule_compares" HeaderStyle-CssClass="gridHeader" AllowAutomaticDeletes="False" AllowAutomaticInserts="False" AllowAutomaticUpdates="False" >
<Columns>
<Telerik:GridDropDownColumn UniqueName="start_and_or" DataField="start_and_or" HeaderText="Comparison Item" ListDataMember="start_and_or" ListTextField="start_and_or" ListValueField="start_and_or"/>
<Telerik:GridDropDownColumn UniqueName="column_name" DataField="column_name" HeaderText="Register Column" ListDataMember="column_names" ListTextField="column_name" ListValueField="column_name"/>
<Telerik:GridDropDownColumn UniqueName="comparison_type" DataField="comparison_type" HeaderText="Comparison Type" ListDataMember="comparison_type" ListTextField="comparison_type" ListValueField="comparison_type"/>
<Telerik:GridBoundColumn UniqueName="comparison_value" DataField="comparison_value" DataType="System.String" HeaderText="Comparison Value" />
</Columns>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="True" />
</ClientSettings>
</Telerik:RadGrid>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = GetEmptyCompareTable();
dt = AddFirstCompareRow(dt);
GetCompareData(dt);
rgRuleCompares.DataBind();
}
}
protected void rgRuleCompares_ItemCommand(object sender, GridCommandEventArgs e)
{
DataTable dt = BuildCompareTableFromGrid();
dt = AddNewCompareRow(dt);
GetCompareData(dt);
}
private DataTable BuildCompareTableFromGrid()
{
int cnt = rgRuleCompares.Items.Count;
DataTable dt = GetEmptyCompareTable();
for (int i = 0; i < cnt; i++)
{
GridEditableItem editedItem = (GridEditableItem)rgRuleCompares.EditItems[0];
string val1 = editedItem.OwnerTableView.DataKeyValues[i]["start_and_or"].ToString();
string val2 = editedItem.OwnerTableView.DataKeyValues[i]["comparison_type"].ToString();
string val3 = editedItem.OwnerTableView.DataKeyValues[i]["comparison_value"].ToString();
string val4 = editedItem.OwnerTableView.DataKeyValues[i]["column_name"].ToString();
DataRow row;
row = dt.NewRow();
row["start_and_or"] = val1;
row["column_name"] = val4;
row["comparison_type"] = val2;
row["comparison_value"] = val3;
dt.Rows.Add(row);
}
return dt;
}
private DataTable GetEmptyCompareTable()
{
DataTable dt = new DataTable();
DataColumn col;
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "start_and_or";
dt.Columns.Add(col);
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "column_name";
dt.Columns.Add(col);
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "comparison_type";
dt.Columns.Add(col);
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "comparison_value";
dt.Columns.Add(col);
dt.TableName = "reg_rule_compares";
return dt;
}
{
DataSet ds = new DataSet();
dt.TableName = "reg_rule_compares";
ds.Tables.Add(dt);
ds = BuildCompareDropDownTables(ds);
rgRuleCompares.DataSource = ds;
}
private DataSet BuildCompareDropDownTables(DataSet ds)
{
DataTable dt = new DataTable();
DataColumn col;
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "comparison_type";
dt.Columns.Add(col);
string[] s = { "Contains", "StartsWith", "EndsWith", "=" };
foreach (string str in s)
{
string field = str.ToString();
DataRow row;
row = dt.NewRow();
row["comparison_type"] = field;
dt.Rows.Add(row);
}
dt.TableName = "comparison_type";
ds.Tables.Add(dt);
dt = new DataTable();
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "column_name";
dt.Columns.Add(col);
string[] s1 = { "Memo", "Payee" };
foreach (string str in s1)
{
string field = str.ToString();
DataRow row;
row = dt.NewRow();
row["column_name"] = field;
dt.Rows.Add(row);
}
dt.TableName = "column_names";
ds.Tables.Add(dt);
dt = new DataTable();
//This adds a column to the table
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "start_and_or";
dt.Columns.Add(col);
string[] s2 = { "START", "AND", "OR" };
foreach (string str in s2)
{
string field = str.ToString();
DataRow row;
row = dt.NewRow();
row["start_and_or"] = field;
dt.Rows.Add(row);
}
dt.TableName = "start_and_or";
ds.Tables.Add(dt);
return ds;
}
private DataTable AddFirstCompareRow(DataTable dt)
{
DataRow row;
row = dt.NewRow();
row["start_and_or"] = "START";
row["column_name"] = "Memo";
row["comparison_type"] = "Contains";
row["comparison_value"] = "";
dt.Rows.Add(row);
return dt;
}
private DataTable AddNewCompareRow(DataTable dt)
{
DataRow row;
row = dt.NewRow();
row["start_and_or"] = "AND";
row["column_name"] = "Memo";
row["comparison_type"] = "Contains";
row["comparison_value"] = "";
dt.Rows.Add(row);
return dt;
}
private void rgRuleCompares_PreRender(object sender, System.EventArgs e)
{
foreach (GridItem item in rgRuleCompares.MasterTableView.Items)
{
if (item is GridEditableItem)
{
GridEditableItem editableItem = item as GridDataItem;
editableItem.Edit = true;
}
}
rgRuleCompares.Rebind();
}
protected void rgRuleCompares_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
}