Is it possible in datagridview with a bindingsource when adding a new row it's copy the last cell name and put a number to it? Copy the name is not a problem but the number after the name i can't get solved. The result i'm looking for is when you press the add button, you get: Sheet 1, Sheet 2, Sheet 3 etc (See image how it should look) with the code i have now you get: Sheet 1, Sheet 11, Sheet 111 etc.

Thanks!

        private void newSheet_Click(object sender, EventArgs e)
    {
        string newItemName;
        string newItemNumber;
        string newItemDrawnBy; 

        if (bindingSource1.Count > 0)
        {
            bindingSource1.MoveLast();
            SheetResource lastItem = bindingSource1.Current as SheetResource;

            newItemName = lastItem.Name + " " + 1; 
            newItemNumber = lastItem.Number;
            newItemDrawnBy = lastItem.Drawn;
        }
        else
        {        
            newItemName = "Sheet";
            newItemNumber = "A-00";
            newItemDrawnBy = "Author";
        }

        SheetResource newItem = new SheetResource();
        newItem.Name = newItemName;
        newItem.Number = newItemNumber;
        newItem.Drawn = newItemDrawnBy;

        bindingSource1.Add(newItem);
    }
  • have you tried to get gridview.items.count and add it to your new row? – Alireza Yadegari 5 mins ago
  • Well, your code is the following lastItem.Name + " " + 1 - if you think about (or had a look while debugging) - you would know/see that lastItem.Name is the Value Sheet 3 - so what you are getting is "Sheet 3" + " " + 1 which result int "Sheet 3 1" since you are only combining strings and not doing any numeric operation - what you want is get the number out of your string and do a numeric operation on it to get the next higher number – Rand Random 3 mins ago

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.