我想要做的是在将记录保存到数据库之前我想在GridView中显示记录,当用户填写文本框并单击按钮时,记录将显示在GridView上,序列号为1,2 ,3 ...... 到目前为止,我已接近这一点。第一个记录成功添加,但添加第二个记录时,DataTable会给出空引用异常。这是正确的方法吗?或者有一个简单的方法来做到这一点。我正在使用Telerik RadGrid for asp.net。
public DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt = new DataTable();
dt.Columns.Add("Sn#");
dt.Columns.Add("type");
dt.Columns.Add("AccountTitle");
dt.Columns.Add("Description");
dt.Columns.Add("CostCenter");
dt.Columns.Add("Debit");
dt.Columns.Add("Credit");
sno.Text = "1";
}
}
protected void radGridView2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dt = AddRow(dt); // call the method to create row
ViewState["dt"] = dt;
dt = (DataTable)ViewState["dt"];
radGridView2.DataSource = dt;
}
private DataTable AddRow(DataTable dt)
{ // method to create row
DataRow dr = dt.NewRow();
dr[0] = sno.Text;
dr[1] = type.Text;
dr[2] = htitle.Text;
dr[3] = disc.Text;
dr[4] = job.SelectedItem.Text;
dr[5] = drr.Text;
dr[6] = crr.Text;
dt.Rows.Add(dr);
return dt;
}
protected void b1_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
ViewState["dt"] = AddRow(dt);
radGridView2.Rebind();
Session["cttt"] = Convert.ToInt32(sno.Text)+1;
sno.Text = Session["cttt"].ToString();
}
答案 0 :(得分:1)
我已经复制了你的代码并稍微改了一下。由于您dt
宣布两次。我相信您的错误将是DataTable
null
的.aspx
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<div>
<telerik:RadGrid ID="rg" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"></telerik:RadGrid>
<br />
<asp:TextBox ID="sno" runat="server"></asp:TextBox><br />
<asp:TextBox ID="type" runat="server" Text="A"></asp:TextBox><br />
<asp:TextBox ID="htitle" runat="server" Text="B"></asp:TextBox><br />
<asp:TextBox ID="disc" runat="server" Text="C"></asp:TextBox><br />
<asp:DropDownList ID="job" runat="server"><asp:ListItem>Yeah</asp:ListItem></asp:DropDownList><br />
<asp:TextBox ID="drr" runat="server" Text="D"></asp:TextBox><br />
<asp:TextBox ID="crr" runat="server" Text="E"></asp:TextBox><br />
<br />
<br />
<asp:Button ID="b1" runat="server" Text="Click" OnClick="b1_Click" />
</div>
</asp:ScriptManager>
的.cs
private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sno.Text = "1";
}
}
private DataTable CreateDataTable()
{
dt = new DataTable();
dt.Columns.Add("Sn#");
dt.Columns.Add("type");
dt.Columns.Add("AccountTitle");
dt.Columns.Add("Description");
dt.Columns.Add("CostCenter");
dt.Columns.Add("Debit");
dt.Columns.Add("Credit");
return dt;
}
private DataTable AddRow(DataTable dt1)
{ // method to create row
DataRow dr = dt1.NewRow();
dr[0] = sno.Text;
dr[1] = type.Text;
dr[2] = htitle.Text;
dr[3] = disc.Text;
dr[4] = job.SelectedItem.Text;
dr[5] = drr.Text;
dr[6] = crr.Text;
dt1.Rows.Add(dr);
return dt1;
}
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
rg.DataSource = ViewState["dt"] as DataTable;
}
protected void b1_Click(object sender, EventArgs e)
{
DataTable dt1 = ViewState["dt"] != null ? ViewState["dt"] as DataTable : CreateDataTable();
ViewState["dt"] = AddRow(dt1);
rg.Rebind();
Session["cttt"] = Convert.ToInt32(sno.Text) + 1;
sno.Text = Session["cttt"].ToString();
}
答案 1 :(得分:0)
在b1_click事件中,您正在创建一个与全局变量(dt)同名的变量 - 这会导致混淆和错误。
protected void b1_Click(object sender, EventArgs e)
{
//Use the global variable dt
//DataTable dt = (DataTable)ViewState["dt"];
dt = (DataTable)ViewState["dt"];
ViewState["dt"] = AddRow(dt);
radGridView2.Rebind(); //Note this would call the NeedDataSource event
Session["cttt"] = Convert.ToInt32(sno.Text)+1;
sno.Text = Session["cttt"].ToString();
}
此处需要注意的另一点是,按钮单击时将调用AddRow方法两次。一次从b1_click事件再到NeedDataSource事件(在radGridView2.Rebind()上触发)