Get Boolean Value Gridview Column

Asked

Viewed 386 times

-1

I have a GridView filling correctly, but I need to take the value of column 5, which is of type BIT in the database (SQL Server).

In every way I try, informs error, I tried to pass to a variable of type bool, move on to a CheckBox and yet the error persists.

Follows the code:

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
 {
     txtid.Text = (GridView1.SelectedRow.Cells[0].Text);
     conta = (GridView1.SelectedRow.Cells[2].Text);
     bool Insertar = ((CheckBox)(GridView1.SelectedRow.Cells[5].FindControl("quitado"))).Checked;
     if (Insertar == true)
     {
         cbquitado.Checked = true;
     }
}

I tried that way too:

bool quitado = Convert.ToBoolean(GridView1.SelectedRow.Cells[5].Text);

How can I get the value of the column by selecting the row in the GridView?

2 answers

2


Try this way (updated):

CheckBox Insertar = (CheckBox)GridView1.SelectedRow.Cells[5].Controls[0];
if(Insertar.Checked == true)
{
     //seu código
}

I assigned it directly to the police, but you can do it your way:

if(Insertar.Checked == true)
{
    cbquitado.Checked = true;
}
  • I tried this way Ismael, and it occurred to me error in this line: cbquitado. Checked = Insert.Checked; Error: Undefined object reference for an instance of an object.

  • I was already editing the answer telling you to check first if the SelectedRow is or is not null. This property is only valid for when you click at least one line. It starts as null.

  • rsrs I show the field in a message, but it comes null, only q with a normal string field in the same command, normal appears.

  • In his gridview he’s not the Checkboxfield type?

  • Exact. <Asp:Checkboxfield Datafield="quitado" Headertext="Quitado"> <Headerstyle Horizontalalign="Center" /> <Itemstyle Horizontalalign="Center" /> </Asp:Checkboxfield> I tried this way q informed me, and shows me the same error on the line: cbquitado. Checked = Insert.Checked. Error: Undefined object reference for an instance of an object.

  • 1

    I managed to solve Ismael by performing a select through the ID that I can get normally, and solved the problem, thank you very much.

Show 1 more comment

0

I created a form and put in it a dataGridView, with two fields(cellula1 and celua2) Two more checkbox and a button I clicked on dataGridView1 and I checked properties and looked for the event click, and then a double qlique to generate the event.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        DataGridView1.Rows[0].Cells[0].Value = "true";
        DataGridView1.Rows[0].Cells[1].Value = "false";
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        checkBox1.Checked = Convert.ToBoolean(DataGridView1.Rows[e.RowIndex].Cells["celula1"].Value.ToString());
        checkBox2.Checked = Boolean.Parse(DataGridView1.Rows[e.RowIndex].Cells["celula2"].Value.ToString());

    }
}
}

Browser other questions tagged

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