Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
clicking on checkbox it has to enable the same row ,next column of textbox of datagridview .but here its enabling the next row textbox.how do i restrict it to enable only the same row textbox.using c# code

What I have tried:

public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
           dataGridView1.Columns["AMOUNT"].Visible = false;

           //  LoadSerial();

       }
       /*   private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
          {
              LoadSerial();
          }*/

       // int COUNTER = 0;
       private void button1_Click(object sender, EventArgs e)
       {


           //ADDDATA(textBox2.Text, textBox1.Text);
           //string EMINITIESNAME = textBox2.Text;
           //string DESCRIPTION = textBox1.Text;
           int row = 0;
           dataGridView1.Rows.Add();
           row = dataGridView1.Rows.Count - 2;
           dataGridView1["AMENITIESNAME", row].Value = textBox2.Text;
           dataGridView1["DESCRIPTION", row].Value = textBox1.Text;
           LoadSerial();

       }
       private void LoadSerial()
       {
           int i = 1;
           foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               row.Cells["ID"].Value = i; i++;
           }

       }


       public void ADDDATA(string AMENITIESNAME, string DESCRIPTION)
       {
           int row = 0;
           dataGridView1.Rows.Add();
           row = dataGridView1.Rows.Count - 2;
           dataGridView1["AMENITIESNAME", row].Value = textBox2.Text;
           dataGridView1["DESCRIPTION", row].Value = textBox1.Text;


       }




       private void Form1_Load(object sender, EventArgs e)
       {

       }

       private void textBox1_TextChanged(object sender, EventArgs e)
       {

       }



       private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {


           foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
           {
               if (dataGridRow.Cells["SELECT"].Value != null && (bool)dataGridRow.Cells["SELECT"].Value)
               {

                   dataGridView1.Columns["AMOUNT"].Visible = true;


               }

               else if (dataGridRow.Cells["SELECT"].Value == null)
               {
                   // Unchecked
               }

           }
       }
Posted
Updated 17-May-17 0:28am
Comments
Afzaal Ahmad Zeeshan 17-May-17 5:18am    
So, where is the event handler for that checkbox?
saimanisha 17-May-17 5:30am    
THE chekcbox is a columntype in datagridview .
using grid event CellContentClick i checked whether checkbox is checked or not.but i want to make next column textbox to visile state only if checkbox is checked ..but as per the above code when i click second checkbox its displaying next row textbox column.but here i want to make visible only the current row.

1 solution

First from the DataGridView property, change "AllowUserToAddRows" from "True" to "False"

Changed Snippets:

Change "row = dataGridView1.Rows.Count -2;" to "row = dataGridView1.Rows.Count -1;" in your methods.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                var curCell = dataGridView1.CurrentCell;
                if (curCell.Value == null || (bool)curCell.Value == false)
                {
                    curCell.Value = true;
                }
                else
                {
                    curCell.Value = false;
                }

                if ((bool)curCell.Value)
                {

                    dataGridView1.Columns["AMOUNT"].Visible = true;
                }
                else
                {
                    dataGridView1.Columns["AMOUNT"].Visible = false;
                }
            }
        }


If this works, please mark this as a solution.
 
Share this answer
 
Comments
saimanisha 17-May-17 6:58am    
the textbox of amount column must not be visible till i click on checkbox .your answer is working partially like its correct but according to my requirement when i click the checkbox of column 4 row1 ,textbox of column 5 and row1 must be visible.
saimanisha 17-May-17 7:06am    
iam getting a problem that is second time when am adding data to datagridview without clicking on check box its showing column amount textbox in datagridview.how to overcome this problem
Zunayed Shahriar 18-May-17 2:36am    
New Changes

New Method:
private void ResetSelection()
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells["SELECT"].Value = false;
}
}

Changed Methods:

private void button1_Click(object sender, EventArgs e)
{


//ADDDATA(textBox2.Text, textBox1.Text);
//string EMINITIESNAME = textBox2.Text;
//string DESCRIPTION = textBox1.Text;
int row = 0;
dataGridView1.Rows.Add();
row = dataGridView1.Rows.Count -1;
dataGridView1["AMENITIESNAME", row].Value = textBox2.Text;
dataGridView1["DESCRIPTION", row].Value = textBox1.Text;
dataGridView1.Columns["AMOUNT"].Visible = false;
LoadSerial();
ResetSelection();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
System.Threading.Thread.Sleep(200);
if (e.RowIndex > -1 && dataGridView1.CurrentCell == dataGridView1.Rows[e.RowIndex].Cells["SELECT"])
{
ResetSelection();
if (dataGridView1.Columns["AMOUNT"].Visible == false)
{
dataGridView1.CurrentCell.Value = true;
dataGridView1.Columns["AMOUNT"].Visible = true;
}
else
{
dataGridView1.Columns["AMOUNT"].Visible = false;
dataGridView1.CurrentCell.Value = false;
}
}
}

Try this. If there is any problem, then let me know.
saimanisha 18-May-17 3:19am    
its working same as yesterday code..when i click checkbox of current row it must enable textbox of another column of the same row..but here its working like its enabling disabling the entire column of textbox which is nothing but amount column..
Zunayed Shahriar 18-May-17 3:27am    
You just cannot hide a cell. What you can do is this.

Full new source:

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 STS_RAM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

// LoadSerial();

}
/* private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
LoadSerial();
}*/

// int COUNTER = 0;
private void button1_Click(object sender, EventArgs e)
{


//ADDDATA(textBox2.Text, textBox1.Text);
//string EMINITIESNAME = textBox2.Text;
//string DESCRIPTION = textBox1.Text;
int row = 0;
dataGridView1.Rows.Add();
row = dataGridView1.Rows.Count -1;
dataGridView1["AMENITIESNAME", row].Value = textBox2.Text;
dataGridView1["DESCRIPTION", row].Value = textBox1.Text;
dataGridView1["AMOUNT", row].ReadOnly = true;
dataGridView1["AMOUNT", row].Style.BackColor = Color.Black;
LoadSerial();
}
private void LoadSerial()
{
int i = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells["ID"].Value = i; i++;
}

}


public void ADDDATA(string AMENITIESNAME, string DESCRIPTION)
{
int row = 0;
dataGridView1.Rows.Add();
row = dataGridView1.Rows.Count -1;
dataGridView1["AMENITIESNAME", row].Value = textBox2.Text;
dataGridView1["DESCRIPTION", row].Value = textBox1.Text;
}




private void Form1_Load(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}



private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
System.Threading.Thread.Sleep(200);
var amountCell = dataGridView1.Rows[e.RowIndex].Cells["AMOUNT"];
if (e.RowIndex > -1 && dataGridView1.CurrentCell == dataGridView1.Rows[e.RowIndex].Cells["SELECT"])
{
if (amountCell.ReadOnly == true)
{
dataGridView1.CurrentCell.Value = true;
amountCell.ReadOnly = false;
amountCell.Style.BackColor = Color.White;
}
else
{
dataGridView1.CurrentCell.Value = false;
amountCell.ReadOnly = true;
amountCell.Style.BackColor = Color.Black;
}
}
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900