当前位置:编程学习 > C#/ASP.NET >>

C#里面怎么更改DATAGRID里面一个单元格的内容。



如图,当我查找text1里跟name字段去比较,如果找到了,就修改它后面luru字段里的内容,改为“已经找到”(原来的数据库里是空的),并且只要这次查询修改就可以了,数据库里面不用更改的。

我的需求是这样的
如果找到,并且后面luru字段是空的话就修改luru那里为“已经找到”
如果找到,但是后面luru字段已经是“已经找到”的话,就MSG"已经找过一遍拉"
如果没找到,就MSG“找不到”

请问这个按钮2要怎么写啊  

不懂C# 啊,谢谢大伙了 --------------------编程问答-------------------- this.dataGridView1.Rows[0].Cells[columnName].Value = 值;//这样就可以直接修改某个单元格的值。Rows[0],行的索引,可以任意指定,只要不超过datagridview的行的集合个数即可。 --------------------编程问答-------------------- 那要怎么样呢,,接在查询语句后面吗
string strSQL = "select * from biao1 where name = '" + textBox1.Text + "'";

--------------------编程问答-------------------- dataGridView1.Rows[rowindex].Cells[colindex].Value --------------------编程问答-------------------- private void button2_Click(object sender, EventArgs e)
{
   string name = this.datagridview1.Rows[dgvVideoList.CurrentCell.RowIndex].Cells["name"].Value.ToString();
   if(name==this.text1.Text)
   {
       if(this.datagridview1.Rows[dgvVideoList.CurrentCell.RowIndex].Cells["luru"].Value=="已经找到")
{
    MessBoxage.Show("已经找过一遍拉");
}
else
{
this.datagridview1.Rows[dgvVideoList.CurrentCell.RowIndex].Cells["luru"].Value=="已经找到")
}
   }   
}

直接在回复里面写的,稍微处理一下应该可以的。 --------------------编程问答--------------------

private void button2_Click(object sender, EventArgs e)
        {
        string name = this.dataGridView1.Rows[dgvVideoList.CurrentCell.RowIndex].Cells["name"].Value.ToString();
        if(name==this.textBox1.Text)
            {
                if(this.dataGridView1.Rows[dgvVideoList.CurrentCell.RowIndex].Cells["luru"].Value=="已经找到")
                    {
                        MessageBox.Show("已经找过一遍拉");
                     }
                  else
                {
                    this.dataGridView1.Rows[dgvVideoList.CurrentCell.RowIndex].Cells["luru"].Value == "已经找到";
                }
             }   
        }






只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句
当前上下文中不存在名称“dgvVideoList”
还有这两个提示错误,,不会改了

 
--------------------编程问答-------------------- --------------------编程问答-------------------- dgvVideoList是你的那个DATAGRIDView名称。

 this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["luru"].Value == "已经找到";改成 this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["luru"].Value = "已经找到";
你自己改下吧 --------------------编程问答--------------------
引用 7 楼 zhangyi2013 的回复:
dgvVideoList是你的那个DATAGRIDView名称。

 this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["luru"].Value == "已经找到";改成 this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["luru"].Value = "已经找到";
你自己改下吧


嗯 谢谢 
修改后可以运行了,,

不过只能查找指针选中的那一行,没有查找整个datagview1,

是不是要加循环啊? --------------------编程问答-------------------- if(this.datagridview1.RowCount>0)
{
 for(int i=0;i<this.datagridview1.RowCount;i++)
{
   
}
}

循环是这样写的,将原来的代码dgvVideoList.CurrentCell.RowIndex改成i就行了,不过你的name值是唯一的吗? --------------------编程问答--------------------
引用 9 楼 zhangyi2013 的回复:
if(this.datagridview1.RowCount>0)
{
 for(int i=0;i<this.datagridview1.RowCount;i++)
{
   
}
}

循环是这样写的,将原来的代码dgvVideoList.CurrentCell.RowIndex改成i就行了,不过你的name值是唯一的吗?






555,还是不行 ,纷纷你再帮我改改,,

 private void button2_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount > 0)
            {
                for (int i = 0; i < this.dataGridView1.RowCount; i++)
                {
                    string name = this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["name"].Value.ToString();
                    if (name == this.textBox1.Text)
                    {

                        if (this.dataGridView1.Rows[i].Cells["luru"].Value == "已经找到")
                        {
                            MessageBox.Show("已经找过一遍拉");
                        }
                        else
                        {

                            this.dataGridView1.Rows[i].Cells["luru"].Value = "已经找到";
                        }
                    }
                        //if (name !=this.textBox1.Text)
                    else
                        {
                            MessageBox.Show("没有这个选项");
                        }

                    
                }
            }
        }
--------------------编程问答--------------------
引用 10 楼 jainkai 的回复:
Quote: 引用 9 楼 zhangyi2013 的回复:

if(this.datagridview1.RowCount>0)
{
 for(int i=0;i<this.datagridview1.RowCount;i++)
{
   
}
}

循环是这样写的,将原来的代码dgvVideoList.CurrentCell.RowIndex改成i就行了,不过你的name值是唯一的吗?






555,还是不行 ,纷纷你再帮我改改,,

 private void button2_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount > 0)
            {
                for (int i = 0; i < this.dataGridView1.RowCount; i++)
                {
                    string name = this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["name"].Value.ToString();
                    if (name == this.textBox1.Text)
                    {

                        if (this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["luru"].Value == "已经找到")
                        {
                            MessageBox.Show("已经找过一遍拉");
                        }
                        else
                        {

                            this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["luru"].Value = "已经找到";
                        }
                    }
                        //if (name !=this.textBox1.Text)
                    else
                        {
                            MessageBox.Show("没有这个选项");
                        }

                    
                }
            }
        }


这样可以了吧  --------------------编程问答--------------------
引用 11 楼 qyj2009 的回复:
Quote: 引用 10 楼 jainkai 的回复:

Quote: 引用 9 楼 zhangyi2013 的回复:

if(this.datagridview1.RowCount>0)
{
 for(int i=0;i<this.datagridview1.RowCount;i++)
{
   
}
}

循环是这样写的,将原来的代码dgvVideoList.CurrentCell.RowIndex改成i就行了,不过你的name值是唯一的吗?






555,还是不行 ,纷纷你再帮我改改,,

 private void button2_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount > 0)
            {
                for (int i = 0; i < this.dataGridView1.RowCount; i++)
                {
                    string name = this.dataGridView1.Rows[i].Cells["name"].Value.ToString();
                    if (name == this.textBox1.Text)
                    {

                        if (this.dataGridView1.Rows[i].Cells["luru"].Value == "已经找到")
                        {
                            MessageBox.Show("已经找过一遍拉");
                        }
                        else
                        {

                            this.dataGridView1.Rows[i].Cells["luru"].Value = "已经找到";
                        }
                    }
                        //if (name !=this.textBox1.Text)
                    else
                        {
                            MessageBox.Show("没有这个选项");
                        }

                    
                }
            }
        }


这样可以了吧 


ca  好像应该是这样的 --------------------编程问答-------------------- 用委托事件处理 CellEndEdit --------------------编程问答-------------------- 你看11楼的,是那样的,你也太不细心了吧 -_- ! --------------------编程问答-------------------- 还是不对  循环有问题 --------------------编程问答--------------------
引用 15 楼 jainkai 的回复:
还是不对  循环有问题


有什么问题12楼是正确的啊
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,