Paste Value Boolean Column GridView

-1

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

In every way I try, it informs error, I already tried to pass to a variable of type bool , pass to a CheckBox and still the error persists.

Follow 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've tried it too:

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

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

    
asked by anonymous 17.04.2017 / 15:55

2 answers

2

Try this way (updated):

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

I gave the cbquitado direct, but, you can do it your way:

if(Insertar.Checked == true)
{
    cbquitado.Checked = true;
}
    
17.04.2017 / 16:06
0

I created a form and put a dataGridView in it, with two fields (cell1 and cell2) Two more checkBox and one button I clicked on the dataGridView1 and put in properties and looked for the click event, 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());

    }
}
}
    
27.10.2018 / 06:16