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

automatic add row

4 Answers 136 Views
Grid
This is a migrated thread and some comments may be shown as answers.
EJ
Top achievements
Rank 1
EJ asked on 08 Jun 2010, 12:56 PM
i got used to my previous infragistics add row that always appears either at the top or bottom of the grid for adding new records without clicking on a button - how do I do this in radgrid? I have dropdown columns and numeric or text columns

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Jun 2010, 01:50 PM
Hello,

Try the following code snippet in PreRender event in order to show insertform when page loads.

C#:
  
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    {  
        RadGrid1.MasterTableView.IsItemInserted = true
        RadGrid1.MasterTableView.Rebind();  
    } 

Thanks,
Princy.
0
EJ
Top achievements
Rank 1
answered on 08 Jun 2010, 10:21 PM
I'm closer but need to access the inserted item programmatically from a method that is outside of the grid.

my grid's items are all in edit mode by default and I am adding onblur and onfocus events to them to keep track of a client-side update to the footer total and looping through them in a method to see if they need to be saved. I don't know how to access the inserted row from the server side to see if it has a value that needs to be saved.

see the "//TODO check and save inserted row - not sure how to access it" below

here's my current code:
-------------------------------

protected void MyRadGrid_PreRender(object sender, EventArgs e)
        {
           // puts all items into edit mode by default
            for (int i = 0; i < MyRadGrid.PageSize; i++)
            {
                MyRadGrid.EditIndexes.Add(i);
            }
            ///insert mode on
            MyRadGrid.MasterTableView.IsItemInserted = true;
           
            MyRadGrid.Rebind();

            //subtotal
            foreach (GridDataItem dataItem in MyRadGrid.MasterTableView.Items)
            {
                (dataItem["amountgiven"].Controls[0] as RadNumericTextBox).Attributes.Add(
                    "onblur", "update('" + clientID + "'" + "," + "'" + dataItem["amountgiven"].Controls[0].ClientID + "');");
                (dataItem["amountgiven"].Controls[0] as RadNumericTextBox).Attributes.Add(
                    "onfocus", "getInitialValue('" + dataItem["amountgiven"].Controls[0].ClientID + "');");
            }
        }

        string clientID = "";

        protected void MyRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //subtotal
            if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                clientID = footer["amountgiven"].ClientID;
            }
        }
 public void OnSaveData()
        {
            foreach (GridDataItem item in MyRadGrid.EditItems)
            {
                // instantiate an object that has the entity values (nice to do this automatically but will do this manually for now)
                MyEntity i = new MyEntity();

                // get key values
                i.myid = Guid.Parse(item.GetDataKeyValue("myid").ToString());

                // get new values
                Hashtable newValues = new Hashtable();
                item.ExtractValues(newValues);

                bool rowHasChanged = false;

                string newData = "";
                bool dataHasChanged = false;

                HasDataChangedBeforeSave("amountgiven", newValues, item, out newData, out dataHasChanged);
                if (dataHasChanged)
                    rowHasChanged = true;
                decimal n = 0;
                decimal.TryParse(newData, out n);
                i.amountgiven = n;

                // save the changes for this item only if at least one of the editable fields has changed
                if (rowHasChanged)
                    _MyFacade.UpdateAndSave(i);
            }
            //TODO check and save inserted row - not sure how to access it
        }

        private void HasDataChangedBeforeSave(string columnName, Hashtable newValues, GridDataItem item, out string newData, out bool dataHasChanged)
        {
            dataHasChanged = false;
            newData = "";

            string oldData = "";

            if (MyRadGrid.MasterTableView.Items[item.ItemIndex].SavedOldValues[columnName] != null)
                oldData = MyRadGrid.MasterTableView.Items[item.ItemIndex].SavedOldValues[columnName].ToString();
            if (newValues[columnName] != null)
                newData = newValues[columnName].ToString();
            if (oldData != newData)
                dataHasChanged = true;
        }
0
Accepted
Princy
Top achievements
Rank 2
answered on 09 Jun 2010, 06:55 AM
Hello,

In order to get the value in insert mode,you have to access the control first and then access the value .

C#:
 public void OnSaveData() 
    { 
        GridEditFormInsertItem item = (GridEditFormInsertItem)RadGrid1.MasterTableView.GetInsertItem(); 
        TextBox txt = (TextBox)item["ColumnUniqueName"].Controls[0];  //access the control 
        string value = txt.Text;  //get the value 
    } 

Thanks,
Princy.
0
EJ
Top achievements
Rank 1
answered on 09 Jun 2010, 12:09 PM
thanks I think that handles it!
Tags
Grid
Asked by
EJ
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
EJ
Top achievements
Rank 1
Share this question
or