希望人能解决一下,关于datagrid的再绑定问题。

alefs 2002-05-08 04:20:38
我遇到一个头疼的问题,已经两天了。
我将后台数据绑定到一个DATAGRID,其中我在DATAGRID中加入了许多TEXTBOX 和 dropdownlist控件,然后在WEB端让客户可以修改和选择选择数据。
我认为datagrid中的数据可以用MYDATASET = (DataSet)MyDataGrid.DataSource;得到,然后对dataset进行操作就可以了,但系统报错:“未将对象引用设置到对象的实例。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。”
这样我又自己生成了一个新的dataset,将datagrid中的数据一行行取到dataset中,确认无误,新的DATASET中的值对,同datagrid中一致。但我用:
MyDataGrid.DataSource = getDataFromGridToDataSet().Tables["Customers"].DefaultView;
MyDataGrid.DataBind();
重新绑定时,刚才的错误信息又出现了,我想问一下。 我的相关代码如下:
private DataSet getDataFromGridToDataSet()
{
DataSet myDataSet = new DataSet("myDataSet");
DataTable tCust = new DataTable("Customers");
DataColumn cCustID = new DataColumn("RemoveID", typeof(string));
DataColumn cCode = new DataColumn("cCode", typeof(string));
DataColumn cCustName = new DataColumn("CustName",typeof(string));
DataColumn cSl = new DataColumn("cSl",typeof(string));
DataColumn cUnit = new DataColumn("cUnit",typeof(string));
DataColumn cPrice = new DataColumn("cPrice",typeof(string));
DataColumn cPh = new DataColumn("cPh",typeof(string));
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCode);
tCust.Columns.Add(cCustName);
tCust.Columns.Add(cSl);
tCust.Columns.Add(cUnit);
tCust.Columns.Add(cPrice);
tCust.Columns.Add(cPh);


myDataSet.Tables.Add(tCust);
for (int i = 0; i < 8; i++)
{
CheckBox Cb= (CheckBox)MyDataGrid.Items[i].Cells[0].Controls[1];
TextBox TbCode = (TextBox)MyDataGrid.Items[i].Cells[1].Controls[1];
Label TbCN = (Label)MyDataGrid.Items[i].Cells[3].Controls[1];
TextBox Tbsl = (TextBox)MyDataGrid.Items[i].Cells[4].Controls[1];
DropDownList Tbunit = (DropDownList)MyDataGrid.Items[i].Cells[5].Controls[1];
DropDownList Tbprice = (DropDownList)MyDataGrid.Items[i].Cells[6].Controls[1];
TextBox Tbph = (TextBox)MyDataGrid.Items[i].Cells[7].Controls[1];

DataRow newRow1;
newRow1 = tCust.NewRow();
tCust.Rows.Add(newRow1);
if (Cb.Checked == true)
{
tCust.Rows[i][0] = "1";
}
else
{
tCust.Rows[i][0] = "0";
}
tCust.Rows[i][1] = TbCode.Text;
tCust.Rows[i][2] = TbCN.Text;
tCust.Rows[i][3] = Tbsl.Text;
tCust.Rows[i][4] = Tbunit.SelectedItem.Text;
tCust.Rows[i][5] = Tbprice.SelectedItem.Text;
tCust.Rows[i][6] = Tbph.Text;
}
return myDataSet;
}

private void Button2_Click(object sender, System.EventArgs e)
{


MyDataGrid.DataSource = getDataFromGridToDataSet().Tables["Customers"].DefaultView;
MyDataGrid.DataBind();
}


...全文
36 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
niwalker 2002-05-12
  • 打赏
  • 举报
回复
你的数据不等于datasource也在,那是因为viewstate在起作用。
alefs 2002-05-10
  • 打赏
  • 举报
回复
谢谢各位:

Web Page的无状态性?是什么意思?如果是当button2的click事件发出时,将产生一个Postback,Grid的DataSource就没有的话,我刚才在GRID上输入的数据就应该丢失呀,但它还在,另外,我手工将GRID中的数据用DATASET保存了一次,再绑定时还是同样的错误,为什么?

private void Page_Load(object sender, System.EventArgs e)
{

ServiceCompanyInfo SC = new ServiceCompanyInfo();
Label5.Text =SC.CompanyInfoText() + "入库单";// 在此处放置用户代码以初始化页面
if ( ! this.IsPostBack )
{
IniRkd();
}
}

private void IniRkd()
{
RequiredFieldValidator3.Enabled= false;
RequiredFieldValidator4.Enabled = false;
Label10.Visible = false;
Label11.Visible = false;
DwlistDbck.Visible = false;
DwlistDbdw.Visible = false;
ReturnCkdLsh Rklsh = new ReturnCkdLsh();
TBoxRkdhm.Text = Rklsh.ReturnLsh("Rkd","Rkdlsh");
DateTime dt = DateTime.Now;

TBoxRkrq.Text = dt.ToShortDateString();
//入库类型 InInventoryWay
String strsql = " SELECT DISTINCT InTypeCode AS a1, IntypeName AS a2 FROM InInventoryWay " +
" WHERE (InTypeCode IS NOT NULL) AND (IntypeName IS NOT NULL) " +
" ORDER BY InTypeCode ";

BindList(strsql,DwlistRklx,0);
//单位名称 CompanyStructure
strsql = " SELECT DISTINCT DepartmentCode AS a1, DepartmentName AS a2 FROM CompanyStructure " +
" WHERE (DepartmentCode IS NOT NULL) AND (DepartmentName IS NOT NULL) " +
" ORDER BY DepartmentCode ";
BindList(strsql,DwlistRkdw,1);
// //仓库代码 Storehouse
// strsql = " SELECT DISTINCT StorehouseCode AS a1, StorehouseName AS a2 FROM Storehouse " +
// " WHERE (StorehouseCode IS NOT NULL) AND (StorehouseName IS NOT NULL) AND (StorehouseCode = '" + DwlistDbdw.SelectedItem.Value+ "' " +
// " ORDER BY StorehouseCode ";
// BindList(strsql,DwlistRkck,1);
//供货商 Provider
strsql = "Select Distinct CustomID as a1,CustomName as a2 From Provider " +
" Where (CustomID is not NULL) and (CustomName is Not Null) " +
" Order by CustomID ";
BindList(strsql,DwlistProvider,1);
BindGrid(Rklsh.ReturnLsh("Rkd","Rkdlsh"));
}

ICollection CreateDataSource(string strlsh)
{


dt = new DataTable("dt");
DataRow dr;

// dt.Columns.Add(new DataColumn("Rkdlsh", typeof(string)));

for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
// dr[0] = strlsh;
dt.Rows.Add(dr);
}

dv = new DataView(dt);

return dv;
}

protected void BindGrid(string strlsh)
{
MyDataGrid.DataSource= CreateDataSource(strlsh);
MyDataGrid.DataBind();
}
jerryfangsh 2002-05-09
  • 打赏
  • 举报
回复
需要你的Page_Load的代码
niwalker 2002-05-09
  • 打赏
  • 举报
回复
private void Button2_Click(object sender, System.EventArgs e)
{


MyDataGrid.DataSource = getDataFromGridToDataSet().Tables["Customers"].DefaultView;

//在上面语句设置断点,看是否右边的DefaultView为null

MyDataGrid.DataBind();
}

出错的原因有几种可能:(不一定对,你可以自己验证一下)其中的一种可能是由于Web Page的无状态性,当button2的click事件发出是,将产生一个Postback,这时你的Grid的DataSource就没有了
alefs 2002-05-09
  • 打赏
  • 举报
回复
有人知道吗?

110,535

社区成员

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

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

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