最简单的方式 是遍历数据源的数据 dim tb as DataTable tb = CType(dg_request_part.DataSource,DataTable) For i=0 To tb.Rows.Count-1 'do some thing Next
唉,其实不一定要走遍历Datagrid这条路,换一种思路呢?UP timmy3310(tim)
可以通过遍历数据源,用datareader
for(int i =0 ; i<this.newdGrid.Items.Count; i++) { btn = new Button(); if (this.newdGrid.Items[i].Cells[2].Text.ToString()=="報名活動") { this.newdGrid.Items[i].Cells[2].Controls.Clear(); lb = new Label(); lb.Text = this.newdGrid.Items[i].Cells[2].Text.ToString(); lb.ForeColor = Color.Red; this.newdGrid.Items[i].Cells[2].Controls.Add(lb); } btn = (Button)this.newdGrid.Items[i].Cells[5].FindControl("btnDel"); btn.Attributes.Add("OnClick","return getconfirm()"); }
to dragontt(龙人) :我用你的方法出现以下错误信息“未将对象引用设置到对象的实例”,其中我的datagrid是用,以下方法Update上去的。 Dim edit_ds As DataSet edit_ds = CType(Session("ds_request"), DataSet) Dim edit_tabel As DataTable = edit_ds.Tables("request_table") Dim edit_row As DataRow edit_row = edit_tabel.NewRow() edit_row("rp_solution") = Trim(txt_solution.Text) edit_row("rp_part_no") = Trim(txt_part_no.Text) edit_tabel.Rows.Add(edit_row) dg_request_part.DataSource = edit_tabel.DefaultView dg_request_part.DataBind()
是遍历数据源的数据
dim tb as DataTable
tb = CType(dg_request_part.DataSource,DataTable)
For i=0 To tb.Rows.Count-1
'do some thing
Next
{
btn = new Button();
if (this.newdGrid.Items[i].Cells[2].Text.ToString()=="報名活動")
{
this.newdGrid.Items[i].Cells[2].Controls.Clear();
lb = new Label();
lb.Text = this.newdGrid.Items[i].Cells[2].Text.ToString();
lb.ForeColor = Color.Red;
this.newdGrid.Items[i].Cells[2].Controls.Add(lb);
}
btn = (Button)this.newdGrid.Items[i].Cells[5].FindControl("btnDel");
btn.Attributes.Add("OnClick","return getconfirm()");
}
edit_ds = CType(Session("ds_request"), DataSet)
Dim edit_tabel As DataTable = edit_ds.Tables("request_table")
Dim edit_row As DataRow
edit_row = edit_tabel.NewRow()
edit_row("rp_solution") = Trim(txt_solution.Text)
edit_row("rp_part_no") = Trim(txt_part_no.Text)
edit_tabel.Rows.Add(edit_row) dg_request_part.DataSource = edit_tabel.DefaultView
dg_request_part.DataBind()