This is a migrated thread and some comments may be shown as answers.

Binding DataSource is adding a row not in the DataTable

2 Answers 169 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 28 Apr 2011, 05:25 PM
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)
        {
           
        }

2 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 29 Apr 2011, 11:09 AM
Hello Matthew,

Our system indicates that you have opened a support ticket concerning the same issue.
In order to avoid duplicate posts, I suggest that we continue the communication in the support ticket.

All the best,
Mira
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Matthew
Top achievements
Rank 1
answered on 29 Apr 2011, 06:36 PM
Just an update to anyone who reviews this thread.  Resolution was that the default "Add New Record" button was inserting the row.  To fire the ItemCommand event handler without using that default button which was adding the row I  created my own button using the CommandItemTemplate.

<CommandItemTemplate>
             <table width="100%">
                <tr>
                    <td align="left">
                        <asp:linkbutton runat="server" ID="lnkInitInsert" Visible="true" Text="Add New Row" CommandName="UpdateEdited"> </asp:linkbutton>
                    </td>
                    </tr>
                   
             </table>
            </CommandItemTemplate>

Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Mira
Telerik team
Matthew
Top achievements
Rank 1
Share this question
or