单击该行的复选框后,如何在datagridview中启用文本框? [英] How do I enable textbox in datagridview after clicking checkbox of that row ?
本文介绍了单击该行的复选框后,如何在datagridview中启用文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
点击复选框,它必须启用同一行,datagridview的文本框的下一列。但这里启用下一行textbox.how我是否限制它只启用相同的行textbox.using c#code
我尝试过:
公共部分类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);
//字符串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 dataGridView1.Rows中的行)
{
row.Cells [ID]。Value = 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)
{
}
私人空隙dataGridView1_CellContentClick(对象发件人,DataGridViewCellEventArgs E)
{
的foreach(的DataGridViewRow dataGridRow在dataGridView1.Rows)
{
如果(dataGridRow.Cells [ SELECT]值= NULL&安培;!及(布尔)dataGridRow.Cells [ SELECT]值。)
{
dataGridView1.Columns [ AMOUNT]。可见=真;
}
else if(dataGridRow.Cells [SELECT]。Value == null)
{
// Unchecked
}
}
}
解决方案
首先来自DataGridView属性,将AllowUserToAddRows从True更改为False
更改的片段:
更改row = dataGridView1.Rows.Count -2; torow = dataGridView1.Rows.Count -1;在您的方法。
私人无效dataGridView1_CellContentClick(对象发件人,DataGridViewCellEventArgs E)
{
如果(e.RowIndex > -1)
{
var curCell = dataGridView1.CurrentCell;
if(curCell.Value == null ||(bool)curCell.Value == false)
{
curCell.Value = true;
}
其他
{
curCell.Value = false;
}
if((bool)curCell.Value)
{
dataGridView1.Columns [AMOUNT]。Visible = true;
}
else
{
dataGridView1.Columns [AMOUNT]。Visible = false;
}
}
}
如果有效,请将此标记为解决方案。
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 } } }
解决方案
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.
这篇关于单击该行的复选框后,如何在datagridview中启用文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文