Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
710
Monotouch - GridView insert and delete rows
posted

Hi there,

 

I have a simple grid view sample (in monotouch Xamarin), and I need to let the user to delete rows (after select one) and add new rows (after insert text in a textbox that I have outside the grid somewhere in the main view).

So, I’m asking help for:

-          How to add a button, (button “Delete”), to the header/top of grid view

-          How delete the row after selection from the user

-          How to change the background colour of the selected row in the grid

 

-         How to add button, (button “Insert”), to the header/top of grid view

-         How insert the row in index 0

-         What is the best approach? : To let the user to write directly in the grid? Or To open a modal and let the user write and then pass this for the grid?

-          How can I resolve this problem: When I add the grid view to the main view, this makes by textbox and a button (that I have outside somewhere in the main view) to lose focus. It seems blocked and I cannot write in this textbox or click the button. (Without adding the grid view to the main view, the textbox is fine). (?)

 

Can anyone help me with this questions, with examples in c# monotouch? 

thanks

Parents
No Data
Reply
  • 40030
    Verified Answer
    Offline posted

    Hi, 

    Assuming you're using an IGGridViewDataSourceHelper, the code in C# to delete a selected row would look something like this: 

                IGGridViewDataSourceHelper dsh = (IGGridViewDataSourceHelper)_grid.DataSource;
                List<NSObject> data = new List<NSObject> ((NSObject[])dsh.Data);
                IGRowPath selectedRow = _grid.SelectedRowPath ();
                data.RemoveAt (selectedRow.RowIndex);
                dsh.Data = data.ToArray();
                _grid.DeleteRows (new NSObject[] { selectedRow }, IGGridViewAnimation.IGGridViewAnimationAutomatic);

    The code to insert a row at the top of the grid would look like this:

                IGGridViewDataSourceHelper dsh = (IGGridViewDataSourceHelper)_grid.DataSource;
                List<NSObject> data = new List<NSObject> ((NSObject[])dsh.Data);
                Data d = new Data () { Name = "I'm New" };
                data.Insert(0,d);
                dsh.Data = data.ToArray ();
                _grid.InsertRows (new NSObject[] { new IGRowPath (0, 0) }, IGGridViewAnimation.IGGridViewAnimationAutomatic);

    To change the background color for selected rows, you can use your themes. First create a class that derives from IGGridViewThemeDefinition

        public class MyTheme : IGGridViewThemeDefinition
        {
            public override UIColor SelectedCellColor 
            {
                get 
                {
                    return UIColor.Red;
                }
            }
        }

    Then on your gridView set the theme property to an instance of that class:

    _gridView.Theme = new MyTheme ();

    As for adding buttons to the Header of the grid, i'd be happy to help, but I'm not really sure what you're looking to do. Do you have a screenshot of what you're trying to achieve? A lot of iOS applications usually put the Add/Remove buttons in the Navigation bar of their application. For example, if you have an iPhone, if you open up the contacts App, there is a plus button in the upper right corner in the nav bar. Perhaps that would be a better fit in your application. 

    When it comes how the user should enter data into the grid, thats a matter of preference. I really don't know what to tell you here. Perhaps if you described your scenario with a bit more detail we could help you decide. 

    Adding the gridView to your ViewController's view shouldn't have any bearing on the textBox. Perhaps the textBox and button are just appearing underneath the grid? By default the grid's background color is clear. Try setting it to white, and see if you can see your button and textBox still. If not, then the grid is on top of those controls. 

    I hope this helps as a starting point. 

    -SteveZ

Children