如何将文本框值与datagridview值进行比较 [英] how to compare textbox value with datagridview value
本文介绍了如何将文本框值与datagridview值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表单上有一个按钮,它将文本框中的数据添加到datagridview并且它可以工作。
现在我想检查文本框中的年份是否已经在datagridview中,那么它应该是显示一条消息,如果没有,它应该将文本框中的数据插入到gridview中。
i试过这个但是没有用。请帮帮我
private void btnAdd_Click( object sender,EventArgs e)
{
if ( string .IsNullOrEmpty( this .cboYear2.Text))
{
MessageBox.Show( 请选择一年);
return ;
}
for ( int k = 0 ; k < dgvAddrecords.Rows.Count -1; k ++)
if ( this .cboYear2.Text == this .dgvAddrecords.Rows [ k] .Cells [ 7 ]。值)
{
MessageBox.Show( 年份exsist,请选择新年);
return ;
}
else
{
dgvAddrecords.Rows.Add(txtCode2.Text,dtpDate.Text ,txtName2.Text,txtOwnersnane.Text,cboRatetype.Text,txtDistrict.Text,txtTown.Text,cboYear2.Text,txtYearlydues.Text);
int sum = 0 ;
for ( int i = 0 ; i < dgvAddrecords.Rows.Count; ++ i)
{
sum + = Convert.ToInt32(dgvAddrecords.Rows [ i] .Cells [ 8 ]。值);
}
txtAmountdue.Text = sum.ToString();
}
}
解决方案
更改如果:
< pre lang =c#> for ( int k = 0 ; k < dgvAddrecords.Rows.Count; k ++)
if ( this .dgvAddrecords.Rows [k] .Cells [ 7 ]。值!= null )
if ( this .cboYear2。 Text == this .dgvAddrecords.Rows [k] .Cells [ 7 ]。Value.ToString() )
{
MessageBox.Show( 年份存在,请选择新年< /跨度>);
return ;
}
您可以尝试将DataTable用作DataGridView的DataSource。您只需向DataTable添加行,然后刷新DataGridView。
将您的支票部分代码更改为:
for ( int k = 0 ; k < dgvAddrecords.Rows.Count -1; k ++)
{
if ( this .dgvAddrecords.Rows [k] .Cells [ 7 ]。值!= null )
if ( this .cboYear2.Text == this .dgvAddrecords.Rows [k] .Cells [ 7 ]。Value.ToString())
{
MessageBox.Show( The Year exsist,请选择新的一年 );
return ;
}
}
dgvAddrecords.Rows.Add(txtCode2.Text,dtpDate.Text,txtName2.Text,txtOwnersnane.Text,cboRatetype.Text,txtDistrict.Text,txtTown。 Text,cboYear2.Text,txtYearlydues.Text);
int sum = 0 ;
for ( int i = 0 ; i < dgvAddrecords.Rows.Count; ++ i)
{
sum + = Convert.ToInt32(dgvAddrecords.Rows [ i] .Cells [ 8 ]。值);
}
txtAmountdue.Text = sum.ToString();
i am having a button on a form which adds data in a textbox to datagridview and it works.
now i want to check if the year in the textbox is allready in the datagridview then it should display a message or if not it should insert the data in the textbox to the gridview.
i tried this but didnt work. please help me out
private void btnAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.cboYear2.Text))
{
MessageBox.Show("Please select a year");
return;
}
for (int k = 0; k < dgvAddrecords.Rows.Count -1; k++)
if (this.cboYear2.Text == this.dgvAddrecords.Rows[k].Cells[7].Value)
{
MessageBox.Show("The Year exsist, Please select a new year");
return;
}
else
{
dgvAddrecords.Rows.Add(txtCode2.Text, dtpDate.Text, txtName2.Text, txtOwnersnane.Text, cboRatetype.Text, txtDistrict.Text, txtTown.Text, cboYear2.Text, txtYearlydues.Text);
int sum = 0;
for (int i = 0; i < dgvAddrecords.Rows.Count; ++i)
{
sum += Convert.ToInt32(dgvAddrecords.Rows[i].Cells[8].Value);
}
txtAmountdue.Text = sum.ToString();
}
}
解决方案
change if :
for (int k = 0; k < dgvAddrecords.Rows.Count ; k++) if( this.dgvAddrecords.Rows[k].Cells[7].Value!=null) if (this.cboYear2.Text == this.dgvAddrecords.Rows[k].Cells[7].Value.ToString() ) { MessageBox.Show("The Year exists, Please select a new year"); return; }
You could try using DataTable as a DataSource for your DataGridView. You just add row to DataTable, and then refresh DataGridView.
change your check partial code to this :
for (int k = 0; k < dgvAddrecords.Rows.Count -1; k++) { if(this.dgvAddrecords.Rows[k].Cells[7].Value!=null) if (this.cboYear2.Text == this.dgvAddrecords.Rows[k].Cells[7].Value.ToString() ) { MessageBox.Show("The Year exsist, Please select a new year"); return; } } dgvAddrecords.Rows.Add(txtCode2.Text, dtpDate.Text, txtName2.Text, txtOwnersnane.Text, cboRatetype.Text, txtDistrict.Text, txtTown.Text, cboYear2.Text, txtYearlydues.Text); int sum = 0; for (int i = 0; i < dgvAddrecords.Rows.Count; ++i) { sum += Convert.ToInt32(dgvAddrecords.Rows[i].Cells[8].Value); } txtAmountdue.Text = sum.ToString();
这篇关于如何将文本框值与datagridview值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文