checkar checkbox of gridview based on database value

Asked

Viewed 145 times

3

My application needs to release types of accesses, for example, we have 5 branches and such person needs to access content from only 3 branches.

The ADM user, can create or edit new users. When editing I need to load the accesses he already has and also what he may have.

I created a gridview and put all the affiliates there, and in the database we have the information that he already has access, per user.

Creation of the Gv:

  DataTable tblACESSOfilial = new DataTable();
  tblACESSOfilial.Columns.Add("ACESSOFILIAL", Type.GetType("System.String"));
  tblACESSOfilial.Columns.Add("LOCALDEACESSO", Type.GetType("System.String"));

  String[] tipofilial = new String[18];
  tipofilial[0]  = "1-1-1";
  tipofilial[1]  = "1-4-1";
  tipofilial[2]  = "1-5-7";
  tipofilial[3]  = "1-7-6";
  tipofilial[4]  = "1-123-8";
  tipofilial[5]  = "1-124-8";
  tipofilial[6]  = "1-1-2";
  tipofilial[7]  = "1-1-3";
  tipofilial[8]  = "1-1-4";
  tipofilial[9]  = "1-1-6";
  tipofilial[10] = "1-9-126";
  tipofilial[11] = "5-4-1";
  tipofilial[12] = "5-1-4";
  tipofilial[13] = "5-1-6";
  tipofilial[14] = "5-1-7";
  tipofilial[15] = "5-1-8";
  tipofilial[16] = "5-1-9";
  tipofilial[17] = "10-1-2";

  String[] descfilial = new String[18];
  descfilial[0]  = "1-1-1 (Nome)";
  descfilial[1]  = "1-4-1 (Nome)";
  descfilial[2]  = "1-5-7 (Nome)";
  descfilial[3]  = "1-7-6 (Nome)";
  descfilial[4]  = "1-123-8 (Nome)";
  descfilial[5]  = "1-124-8 (Nome)";
  descfilial[6]  = "1-1-2 (Nome)";
  descfilial[7]  = "1-1-3 (Nome)";
  descfilial[8]  = "1-1-4 (Nome)";
  descfilial[9]  = "1-1-6 (Nome)";
  descfilial[10] = "1-9-126 (Nome)";
  descfilial[11] = "5-4-1 (Nome)";
  descfilial[12] = "5-1-4 (Nome)";
  descfilial[13] = "5-1-6 (Nome)";
  descfilial[14] = "5-1-7 (Nome)";
  descfilial[15] = "5-1-8 (Nome)";
  descfilial[16] = "5-1-9 (Nome)";
  descfilial[17] = "10-1-2 (Nome)";

  for (int x = 0; x <= 17; x++)
  {
    DataRow rows = tblACESSOfilial.NewRow();
    rows["ACESSOFILIAL"] = tipofilial[x];
    rows["LOCALDEACESSO"] = descfilial[x];
    tblACESSOfilial.Rows.Add(rows);
  }

  this.dgvACESSOfilial.DataSource = tblACESSOfilial;
  this.dgvACESSOfilial.DataBind();

In the appointment I have is the following:

SELECT CODCOLIGADA, CODTIPOCURSO, CODFILIAL FROM POLIS_NCADMINFILIAL (NOLOCK)" +
        "WHERE CODUSUARIO = '@CODUSUARIO'

This query can return from 0 to 17 lines.

If the content is in the query it makes a check in gridview in the following form:

List<domUsuario> list2 = dao.ListaTodosFilial(this.txtCODUSUARIO.Text);
        foreach (GridViewRow row in this.dgvACESSOfilial.Rows)
        {
          if (list2[row.RowIndex].COLIGADAIN.Count() > 0)
          {
            string local = list2[row.RowIndex].COLIGADAIN + "-" + list2[row.RowIndex].CODTIPOCURSO + "-" + list2[row.RowIndex].CODFILIAL; ==>ERRO AQUI
            if (local.Contains(row.Cells[1].Text))
            {
              CheckBox chkRow = (row.Cells[0].FindControl("chkRowfilial") as CheckBox);
              chkRow.Checked = true;
            }
          }
        }

The problem is that I have returned the error where I identified as "==>ERROR HERE":

The index was out of range. It must be non-negative and smaller than the collection size. r nName of parameter: index

Could someone help me in what I might be doing? I thank you in advance.

2 answers

2

Edited response:

List<domUsuario> list2 = dao.ListaTodosFilial(this.txtCODUSUARIO.Text);

foreach (GridViewRow row in this.dgvACESSOfilial.Rows)
{
    if(row.RowIndex < 0)
        continue;

    var values = list2
         .Where(o => o.COLIGADAIN.Any())
         .Select(o => $"{o.COLIGADAIN}-{o.CODTIPOCURSO}-{o.CODFILIAL}")
         .ToArray();

   if (values.Any(o => o.Contains(row.Cells[1].Text))
   {
       CheckBox chkRow = (row.Cells[0].FindControl("chkRowfilial") as CheckBox);
       chkRow.Checked = true;
   }
}
  • 1

    then, depending on the query it runs and checks on some items and after this error.. ended up not working the way it said

  • 1

    the error is giving why the amount of datagridview lines and items in the list are different

  • 1

    Yes, but I don’t know what way to "ignore" when it’s different.. Because I have to go through being different, so I can give

  • 1

    I edited the answer, see if that’s what you expect..

  • 1

    actually not yet, talking to a guy here from work.. I realized I needed to do two foreach, one for the select query and the other for the gridview and compare and it worked out.. Soon put the answer

  • lambda of values does a foreach on select values, but blz, I hope it works.

  • I’m still young and I don’t know much, I appreciate your help.. during this time I was trying to solve and it worked in another way

Show 3 more comments

1


In my query returns several lines, so I created an index in it to have control:

int index = 0;

        while (reader.Read())
        {
          domUsuario item = new domUsuario();
          item.COLIGADAIN = reader["CODCOLIGADA"].ToString();
          item.CODTIPOCURSO = reader["CODTIPOCURSO"].ToString();
          item.CODFILIAL = reader["CODFILIAL"].ToString();
          item.INDEX = index;

          Lista.Add(item);

          index++;
        }

When checking, for each query line it checks in the whole gridview:

List<domUsuario> list2 = dao.ListaTodosFilial(this.txtCODUSUARIO.Text);

        list2.ForEach(delegate (domUsuario dom)
        {
          string local = list2[dom.INDEX].COLIGADAIN.ToString() + "-" + list2[dom.INDEX].CODFILIAL + "-" + list2[dom.INDEX].CODTIPOCURSO;

          foreach (GridViewRow row in this.dgvACESSOfilial.Rows)
          {
            if (local.Contains(row.Cells[1].Text))
            {
              CheckBox chkRow = (row.Cells[0].FindControl("chkRowfilial") as CheckBox);
              chkRow.Checked = true;
              break;
            }
          }
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.