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

datagridview添加一行

DataGridViewRow dr = dataGridView1.Rows[dataGridView1.NewRowIndex];
dr.Cells[0].Value = cbbshop.Text.Trim();
dr.Cells[1].Value = tbet.Text.Trim();
dataGridView1.Rows.Add(dr);
错在哪? --------------------编程问答--------------------
DataGridViewRow dr = dataGridView1.Rows[dataGridView1.NewRowIndex]; 
dr.Cells[0].Value = cbbshop.Text.Trim(); 
dr.Cells[1].Value = tbet.Text.Trim(); 
dataGridView1.Rows.Add(dr); 
错在哪?
我跟踪了一下dr 结果显示-1
请问该如何添加非数据库的行啊 --------------------编程问答-------------------- 崩溃了我 谁拯救我一下 --------------------编程问答-------------------- 要用DataTable

DataTable dt = new DataTable();

dt.Columns.Add("a", typeof(string));
dt.Columns.Add("b", typeof(string));
DataRow dr = dt.NewRow();
dr[0] = cbbshop.Text.Trim();  
dr[1] = tbet.Text.Trim();  
dt.Rows.Add(dr);
dr = dt.NewRow();

dataGridView1.DataSource = dt; --------------------编程问答-------------------- 不能这样添加吗?

int index=dataGridView1.Rows.Add();
DataGridViewrow dr=dataGridView1.Rows[index];
--------------------编程问答-------------------- dt是否为空啊?
在代码中执行下查询命令可否? --------------------编程问答-------------------- @dancingbit

会报这个错误:不能向没有列的 DataGridView 控件添加行。必须首先添加列。 --------------------编程问答-------------------- datagrodview1.columns.add();

您要先添加列 才能添加行 --------------------编程问答-------------------- 可能是我说的不准确,对大家误导了,我详细说下我的要求
我的datagridview 已经显示了10条记录
这个时候我想再加一条记录,而且我不想对数据库操作,我是这么做的
DataGridViewRow dr = dataGridView1.Rows[dataGridView1.NewRowIndex];//这个地方下标越界了 
dr.Cells[0].Value = cbbshop.Text.Trim(); 
dr.Cells[1].Value = tbet.Text.Trim(); 
dataGridView1.Rows.Add(dr); 

3楼的兄弟这一句行不通
dataGridView1.DataSource = dt;
又绑定了 这肯定对数据库操作了 
如果想对数据库操作 只写个绑定方法Bind()不就可以了么  添加完Bind();


最大的问题就是不想对数据库操作
恳请各位指点迷津 --------------------编程问答-------------------- 真是崩溃了 --------------------编程问答--------------------
引用 4 楼 dancingbit 的回复:
不能这样添加吗? 

C# code
int index=dataGridView1.Rows.Add();
DataGridViewrow dr=dataGridView1.Rows[index];
--------------------编程问答-------------------- 换成这段就可以啦,应该先插入行,在设置数据

DataGridViewRow dr = dataGridView1.Rows[dataGridView1.NewRowIndex]; 
dataGridView1.Rows.Add(dr); 
dr.Cells[0].Value = cbbshop.Text.Trim(); 
dr.Cells[1].Value = tbet.Text.Trim(); 

--------------------编程问答-------------------- 先创建列:
datagridFldInfo.Columns.Add("FieldName", "字段名");
            datagridFldInfo.Columns.Add("FieldType", "字段类型");
            datagridFldInfo.Columns.Add("FieldPre", "精度(长度)");
            datagridFldInfo.Columns.Add("FieldScale", "范围");

然后按行添加字段:
datagridFldInfo.Rows.Add(txtFieldName.Text,CoBFieldType.SelectedItem.ToString(),txtPre.Text,txtscale.Text); --------------------编程问答-------------------- 在原来的table里添加就可以了
DataRow row = ds.Table[0].NewRow();
row[0] = cbbshop.Text.Trim(); 
row[0] = tbet.Text.Trim(); 
ds.Table[0].Add(row);

ds为原来dataGridView1绑定的DataSet --------------------编程问答--------------------  int index = dataGridViewX2.Rows.Add();
 DataGridViewRow dgvr = dataGridView2.Rows[index];
 dgvr.Cells[0].Value = "123";
 dgvr.Cells[1].Value = "234"; --------------------编程问答--------------------  int index = dataGridView2.Rows.Add();
 DataGridViewRow dgvr = dataGridView2.Rows[index];
 dgvr.Cells[0].Value = "123";
 dgvr.Cells[1].Value = "234"; --------------------编程问答-------------------- 看你的DataGridView是否帮定数据
当控件被数据绑定时,无法以编程方式向DataGridView的行集合中添加行 --------------------编程问答--------------------             DataGridViewRow dr = new DataGridViewRow();
            [align=left]dr.CreateCells(dataGridView1);
            dr.Cells[0].Value = "h";
            dr.Cells[1].Value = "234";
            dataGridView1.Rows.Insert(0, dr);      [/align] --------------------编程问答--------------------  DataGridViewRow dr = new DataGridViewRow();

 dr.CreateCells(dataGridView1);

 dr.Cells[0].Value = "h";

 dr.Cells[1].Value = "234";

 dataGridView1.Rows.Insert(0, dr);  
     --------------------编程问答--------------------

 DataGridViewRow row = new DataGridViewRow();
            row.Cells[0].Value = "";
            row.Cells[1].Value = "";
            dataGridView1.Rows.Add(row);
--------------------编程问答-------------------- 问题解决了么? --------------------编程问答-------------------- 今天做例子时写的个操作dgv的例子,写的不好,自己琢磨去
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region "变量定义"
        IList<Person> p = new List<Person>() 
        {
            new Person { Name = "lizeren", Address = "wenzhou", Remarks = "ailili", Sex=SexType.男 } ,
            new Person {Name="wanglili",Address="jilin",Remarks="aizeren",Sex=SexType.女 },
            new Person {Name="xuhongfei",Address="wenzhou",Remarks="py",Sex=SexType.女 },
            new Person("lizeren","wenzhou","ailili","男")
        };
        BindingSource bds = null;
        #endregion


        private void Form1_Load(object sender, EventArgs e)
        {
            bds = new BindingSource();
            bds.DataSource = p;
            this.dataGridView1.DataSource = bds;
        }
        Form2 Frm = null;

        //添加数据

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            if (Frm == null)
            {
                Frm = new Form2();
                Frm.Text = "添加数据";

                #region "事件"
                //添加数据行
                Frm.button1.Click += new EventHandler((sen, eb) =>
                    {
                        if (Frm.PersonValue != null)
                        {
                            bds.Add(Frm.PersonValue);
                            Frm.Close();
                        }
                        else
                        {
                            MessageBox.Show(this, "格式不正确");
                        }//if
                    });
                //
                //关闭按钮
                //
                Frm.button2.Click += new EventHandler((se, eb) =>
                    {
                        Frm.Close();
                    });

                Frm.FormClosed += new FormClosedEventHandler((send, ec) =>
                    {
                        Frm = null;
                    });
                #endregion

                Frm.Show(this);
            }
            else
            {
                Frm.Activate();
            }//if
        }
        //修改数据
        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            if (Frm == null)
            {
                Frm = new Form2();
                Frm.Text = "修改数据";
                if (bds.Current != null)
                {
                    Frm.PersonValue = (Person)bds.Current;
                }

                #region "事件"
                //
                //修改数据
                //
                Frm.button1.Click += new EventHandler((sen, ex) =>
                    {
                        Person CurPer = (Person)bds.Current;
                        Person gp = Frm.PersonValue;
                        if (gp != null)
                        {                            
                            CurPer.Name = gp.Name;
                            CurPer.Address = gp.Address;
                            CurPer.Remarks = gp.Remarks;
                            CurPer.Sex = gp.Sex;
                            bds.CancelEdit();
                            Frm.Close();
                        }
                        else
                        {
                            MessageBox.Show(this, "格式不正确");
                        }
                    });//event
                //
                //关闭Form2
                //
                Frm.button2.Click += new EventHandler((se, eb) =>
                {
                    Frm.Close();
                });
                Frm.FormClosed += new FormClosedEventHandler((send, ec) =>
                {
                    Frm = null;
                });
                #endregion

                Frm.Show(this);
            }
            else
            {
                Frm.Activate();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            IList<Person> pl = (IList<Person>)bds.List;
        }
    }
}
--------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public Person PersonValue
        {
            get
            {
                if (!string.IsNullOrEmpty(txtName.Text)
                    && !string.IsNullOrEmpty(txtAddress.Text)
                    && !string.IsNullOrEmpty(txtRemarks.Text)
                    && !string.IsNullOrEmpty(cmbSex.Text))
                    return new Person(txtName.Text, txtAddress.Text, txtRemarks.Text, cmbSex.Text);
                return null;
            }
            set
            {
                if (value != null)
                {
                    txtName.Text = value.Name;
                    txtAddress.Text = value.Address;
                    txtRemarks.Text = value.Remarks;
                    cmbSex.Text = value.Sex.ToString();
                }
            }
        }
    }
}

另外一个窗体代码 --------------------编程问答-------------------- 气死我了 --------------------编程问答-------------------- 这个操他妈 --------------------编程问答-------------------- 爷是解决不了了 --------------------编程问答-------------------- 淡定
突然发现,有时候有问题解决不了很生气的时候,真的应该看看别人的帖子,比如看到楼上的 haosafe 朋友,突然觉得心里平衡多了  
哇哈哈哈哈哈哈哈哈哈哈哈哈哈     --------------------编程问答-------------------- 好无聊啊,打瞌睡了... --------------------编程问答-------------------- dataGridView1.NewRowIndex 这里有问题
根据MSDN及调试证明:
NewRowIndex 新记录所在行的索引,如果 AllowUserToAddRows 为 false,则为 -1。
在手动添加行的时候不能使用这个属性 --------------------编程问答--------------------             int count = dgvGuide.Rows.Count;
            dgvGuide.Rows.Add();
            dgvGuide["name", count].Value = node.GetValue("name").ToString().Substring(node.GetValue("name").ToString().LastIndexOf('》') + 1);
            dgvGuide["source", count].Value = node.GetValue("source").ToString();
            dgvGuide["freq", count].Value = node.GetValue("freq").ToString();
            dgvGuide["unit", count].Value = node.GetValue("unit").ToString();
            dgvGuide["firstpubDateFirst", count].Value = node.GetValue("firstpubDateFirst").ToString();
            dgvGuide["lastpubDate", count].Value = node.GetValue("lastpubDate").ToString();
            dgvGuide["lastUpDate", count].Value = node.GetValue("lastUpDate").ToString();
            dgvGuide["id", count].Value = node.GetValue("id").ToString();
            dgvGuide["isFreq", count].Value = "false";
            dgvGuide["freqList", count].Value = "";
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,