0

hi I have a DataGridview in with 15 items. Starting from the 5th item, I want to get 14 of them to array. I want to go back to the begin after the last element of datagridview

public partial class exmple : Form
{
    public exmple()
    {
        InitializeComponent();
    }

    private DataGridView MyDw(DataGridView DVG)
    {
        for (int i = 1; i <= 15; i++)
        {
            DataGridViewRow CreateRow = new DataGridViewRow();
            CreateRow.CreateCells(DVG);
            CreateRow.Cells[0].Value = i-1;
            CreateRow.Cells[1].Value = "A"+i;
            DVG.Rows.Add(CreateRow);
        }
        DVG.Columns[0].Width = 50;
        DVG.Columns[1].Width = 25;
        DVG.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        DVG.RowHeadersVisible = false;
        return DVG;

    }

    private void exmple_Load(object sender, EventArgs e)
    {
        MyDw(dataGridView1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int beginrow = 5;
        ArrayList MyArray = new ArrayList { };
        for (int i = 0; i < 14; i++)
        {

            MyArray.Add(dataGridView1.Rows[beginrow + i].Cells[1].Value.ToString().Trim());

        }
    }
}
foradream
  • 81
  • 1
  • 2
  • 6
  • Just FYI, you should try to use `List` over `ArrayList` where possible: https://stackoverflow.com/a/2309699/2957232 – Broots Waymb Nov 12 '19 at 16:08
  • `Starting from the 5th item, I want to get 14 of them to array. I want to go back to the begin after the last element of datagridview`, so what's the issue, I am not seeing one in your post? – Trevor Nov 12 '19 at 16:10
  • The view shouldn't take care of logic. That is what View Model does. If you want to handle button click events use the ICommand in your view model alongside Observable Collections and the INotifyPropertyChanged interface. There is a lot about MVVM to be found. There is absolutely no sense in referring to your control elements to do logical operations for you in your view. It is considered bad design. Looping through indices in your datagrid is not necessary. This is what the magic of binding does for you. You should take a look at MVVM to understand how to build your application properly. – timunix Nov 12 '19 at 16:23
  • @timunix you realise this is a Windows Forms app, yes? Hardly a good fit for MVVM.Not saying [it isn't possible](https://www.codeproject.com/articles/364485/mvvm-model-view-viewmodel-patte), but advising OP to shoehorn your favourite pattern into a platform which has to be coerced into using it is not very helpful. – stuartd Nov 12 '19 at 17:23
  • It doesn't matter if it is Windows Forms or WPF or whatever it is. My point is that looping a datagrid for its values and saving those into an array which houses in the same class as its view is absolutely not recommended and horrible design. And the hard coding with indexing datagrid rows and columns is even worse, not to mention the click events in the view class. – timunix Nov 12 '19 at 17:49

1 Answers1

1

Could do something like this

private void button1_Click(object sender, EventArgs e)
{
    int beginrow = 5;

    int takeCount = 14;
    int count = 0;

    var myList = new List<string>();

    for(var i  = beginrow; i < dataGridView1.RowCount && count < takeCount; i ++)
    {       
        count ++;
        myList.Add(dataGridView1.Rows[beginrow + i].Cells[1].Value.ToString().Trim());

        if(i + 1 == dataGridView1.RowCount) 
        {
            i = -1;
        }
    }
}
austin wernli
  • 1,801
  • 12
  • 15
  • This is spaghetti code and very bad design. It might work for now but if you add button clicks and datagrid logic like this later, it will break for sure and duplicate code is guaranteed. What happens if you add a new column or change one? Or change the logic? You would have to change your duplicate code in several places. Not flexible and not extensible and there is no place for testing with that choice of poor design. – timunix Nov 12 '19 at 16:35
  • What happens anytime you change the logic? What kind of comment is that.. If you change the requirements for a task, obviously code change is needed in most cases.. This answer isn't meant to be flexible, extensibe, or even testible. It is however a starting point for OP to take and make into whatever they need. – austin wernli Nov 12 '19 at 16:44
  • It is constructive critcism because with this design the OP won't make any longterm progress but run into problems which are evitable if warned about them beforehand. In my opinion, warning the OP for taking the wrong approach is the best way. This design doesn't even closely meet the requirements for the SOLID principles. That's why I have to bring up a big warning here. – timunix Nov 12 '19 at 16:52
  • And it is not true that you need to change what is needed if getting new requirements in most cases. It is the contrary. That is the idea of MVVM that you don't need to change your exisiting code, ideally, but only extend it. – timunix Nov 12 '19 at 16:54
  • the idea of MVVM isn't so you `don't need to change your existing code`... It is to provide a separation of concern between user interface and business logic, and overall allows logic to be tested more easily... You are going far beyond the scope of what the OP asked in the question heh... – austin wernli Nov 12 '19 at 16:58
  • I'm sorry for my bad english.code doesn't work I want.for loop must return to the first element datagridview – foradream Nov 12 '19 at 17:04
  • Take a look at DATA BINDING and MVVM in C# in your native language. The next step is to understand SOLID design principles (don't overdo it but consider them). This is the only way to become a good programmer in C# imho. Everything else is just a quickfix. NEVER loop through your control elements like datagrids inside your view. This is extremely bad. – timunix Nov 12 '19 at 17:10
  • @foradream this doesn't do anything with the list, it just gets a list with 14 items starting at index 5 in the datagridview – austin wernli Nov 12 '19 at 17:12