我要崩溃了,关于dataGridView的问题

yunhaiC QQ654777694 2010-09-05 09:36:43
1.我绑定dataGridView数据源,然后运行窗口,初始的时候dataGridView的固定行只有一行,我现在想做的是,不管有没有数据,没有数据就显示固定的行数,比如10行。假如有数据,数据小于10行,那没有滚动条,超过10条dataGridView有滚动条。关键问题就是绑定数据源后默认固定行就是一行,怎么解决?

2.当我设置好上面的问题以后,我要一个add按钮,每次按一下就在已经有数据的尾巴添加一笔新的空白数据,删除同理

3.为什么我绑定数据源以后,表里面有数据,窗口开启的时候dataGridView没有能够加载数据???


请教各位大侠了,昨天周6一直搞到今天早上3点都没解决,也怪我自己赛扬的电脑太慢了
...全文
272 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 h107127999 的回复:]

引用 1 楼 wuyq11 的回复:
form_load中绑定数据到gridview
判断数据源rows.count<10添加10行数据
添加行
DataGridViewRow row = new DataGridViewRow();
row.Cells[0].Value =dt[""].ToString();
row.Cells[1].Value = dt[""].ToString……
[/Quote]
楼上的朋友
如果我表里面没有数据,那执行你的if (dt.Rows.Count == 0 && dataGridView1.DataSource == bindingSource1)就会报错,没办法确定dataGridView1的样子
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
h107127999 2010-09-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
form_load中绑定数据到gridview
判断数据源rows.count<10添加10行数据
添加行
DataGridViewRow row = new DataGridViewRow();
row.Cells[0].Value =dt[""].ToString();
row.Cells[1].Value = dt[""].ToString();
this.dataG……
[/Quote]
把数据源设为Null
h107127999 2010-09-05
  • 打赏
  • 举报
回复
 DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{

Bind();
if (dt.Rows.Count == 0 && dataGridView1.DataSource == bindingSource1)
{
dataGridView1.DataSource = null;
for (int i = 0; i < 10; i++)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1);
row.Cells[0].Value = "";
row.Cells[1].Value = "";
row.Cells[2].Value = "";
row.Cells[3].Value = "";
dataGridView1.Rows.Add(row);
}

}
else if (dt.Rows.Count <= 10)
{
dataGridView1.ScrollBars = ScrollBars.None;
}
else
{
dataGridView1.ScrollBars = ScrollBars.Both;
}
}

public void Bind()
{
SqlConnection conn = new SqlConnection(@"Data Source=SYC\SQLEXPRESS;Initial Catalog=db_28;Integrated Security=True");
conn.Open();
SqlDataAdapter dap = new SqlDataAdapter("select * from tb_01", conn);
dt = new DataTable();
dap.Fill(dt);
conn.Close();
bindingSource1.DataSource = dt;
dataGridView1.DataSource = bindingSource1;
}
h107127999 2010-09-05
  • 打赏
  • 举报
回复
我的datagridview编辑了4个column

下面是第一个问题的代码

DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{

Bind();
if (dt.Rows.Count == 0 && dataGridView1.DataSource == bindingSource1)
{
dataGridView1.DataSource = null;
for (int i = 0; i < 10; i++)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1);
row.Cells[0].Value = "";
row.Cells[1].Value = "";
row.Cells[2].Value = "";
row.Cells[3].Value = "";
dataGridView1.Rows.Add(row);
}

}
else if (dt.Rows.Count <= 10)
{
dataGridView1.ScrollBars = ScrollBars.None;
}
else
{
dataGridView1.ScrollBars = ScrollBars.Both;
}
}

public void Bind()
{
SqlConnection conn = new SqlConnection(@"Data Source=SYC\SQLEXPRESS;Initial Catalog=db_28;Integrated Security=True");
conn.Open();
SqlDataAdapter dap = new SqlDataAdapter("select * from tb_01", conn);
dt = new DataTable();
dap.Fill(dt);
conn.Close();
bindingSource1.DataSource = dt;
dataGridView1.DataSource = bindingSource1;
}
dancingbit 2010-09-05
  • 打赏
  • 举报
回复
直接在作为数据源的DataTable中添加记录,自然会显示在DataGridView中的。
  • 打赏
  • 举报
回复

public Materiel()
{
InitializeComponent();

//dataGridView1.ColumnCount = 6;
//dataGridView1.RowCount = 6;

mySelectCommand = myConnection.CreateCommand();
myInsertCommand = myConnection.CreateCommand();
myDeleteCommand = myConnection.CreateCommand();
myUpdateCommand = myConnection.CreateCommand();

mySelectCommand.CommandText = "SELECT materiel_Id, materielType, materielPrice,materielQuantity,materielStuff,materielLength,materielWidth,materielHeight,materielColor " +
"FROM materiel " +
"ORDER BY materiel_Id";

mySqlDataAdapter.SelectCommand = mySelectCommand;

mySqlDataAdapter.Fill(myDataSet, "materiel");

dataGridView1.DataSource = myDataSet.Tables["materiel"];

if (dataGridView1.RowCount < 10)
{
DataGridViewRow dr = dataGridView1.Rows[dataGridView1.NewRowIndex];
dataGridView1.Rows.Add(dr);
}
}

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

似乎绑定了以后就不能操做了,请教了
wuyq11 2010-09-05
  • 打赏
  • 举报
回复
form_load中绑定数据到gridview
判断数据源rows.count<10添加10行数据
添加行
DataGridViewRow row = new DataGridViewRow();
row.Cells[0].Value =dt[""].ToString();
row.Cells[1].Value = dt[""].ToString();
this.dataGridView1.Rows.Add(row);

DataGridViewRow dr = dataGridView1.Rows[dataGridView1.NewRowIndex];
dataGridView1.Rows.Add(dr);
tossgoon 2010-09-05
  • 打赏
  • 举报
回复
给绑定的数据源设置默认值.
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 mmc71990242 的回复:]

j=mydataset.Table["表名"].Rows.Count ;
if (j< 10)
{

for (int i = 1; i < 10 - j; i++)
{
mydatase……
[/Quote]
并不是所有的数据都可以 ""这样填的,我decimal类型也这样填就会报错,如果我填0,那刚开启窗口的时候就一大堆0,那样显示还是不对啊
mmc71990242 2010-09-05
  • 打赏
  • 举报
回复
j=mydataset.Table["表名"].Rows.Count ;
if (j< 10)
{

for (int i = 1; i < 10 - j; i++)
{
mydataset.Table["表名"].Rows.Add(" ");
}

}
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.AllowUserToAddRows = false;
这样就可以了吧,直接操作DataSet,判断DataSet的行数,然后再给datagridview.
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 h107127999 的回复:]

我的datagridview编辑了4个column

下面是第一个问题的代码
C# code

DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{

Bind();
if (dt.Rows.Count ……
[/Quote]
麻烦你这样的方法,if (dt.Rows.Count == 0 && dataGridView1.DataSource == bindingSource1)
假如说我绑定的里面有3行数据,你规划好了dataGridView1的格式,10行,但是解绑以后规划好格式再显示那三方资料就没有了啊
【版本信息】:正式版 1.1.4 【发布时间】:2010.11.17 【新增功能】: Mbalib的下载支持 文件名的修改 免费版引入了一个弹窗广告 【功能说明】:       下载单一文件       搜索文件       深度遍历       下载批量文件       管理下载队列       管理所有下载数据       开始下载       停止下载       删除/保留图片等资源       系统设置       软件更新       专业版/企业版认证 【新增功能】:       1.引入了Sqlite管理数据功能       2.引入了专业版授权功能       3.引入了异步数据源       4.引入了深度遍历功能       5.引入了程序压缩,减少了程序的大小       6.增加了双击选中项打开百度或豆丁的查看页面       7.完善了帮助文件(实在是因为csdn的Hi功能经常崩溃) 【修正功能】:       1.修正了下载完成的下载列表反向的问题       2.修正了DataGridView某些情况造成红叉以及对象不存在的问题       3.尽可能的避免了重复下载(用户重复添加相同文件到下载列表中),自动剔除了重复项       4.修正了1.1版本中粘贴Url崩溃的情况(由于sqlite非空引起),修正了异步数据源中删除数据的异常(异步数据及sqlite引起)       5.修正了对win7及64位系统的支持       6.修正了豆丁下载原始文件验证码逻辑错误       7.增加了依赖项检查

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧