我根据选择从gridview中选择记录并将其传递给datatable但不起作用。它抛出错误并且无法正常工作,如何根据frop down选择复制这些行?
代码:
protected void btnGenerateReport_Click(object sender, EventArgs e)
{
dtRoom.Columns.Add(new DataColumn("ID", typeof(Int64)));
dtRoom.Columns.Add(new DataColumn("RecievingDate", typeof(string)));
dtRoom.Columns.Add(new DataColumn("FromMobileNo", typeof(string)));
dtRoom.Columns.Add(new DataColumn("Message", typeof(string)));
dtRoom.Columns.Add(new DataColumn("IsComplaint", typeof(short)));
for (int i = 0; i <= GridViewSmsComplaints.Rows.Count; i++)
{
//drw = dtRoom.NewRow();
DropDownList IsValid = (DropDownList) GridViewSmsComplaints.Rows[i].FindControl("ddlValidity");
if (IsValid.SelectedValue == "1")
{
DataRow datarw = null;
dtRoom = new DataTable();
datarw = dtRoom.NewRow();
for (int j = 1; j < GridViewSmsComplaints.Rows[i].Cells.Count; j++)
{
datarw[j] = GridViewSmsComplaints.Rows[i].Cells[j].Text;
}
dtRoom.Rows.Add(datarw);
}
}
GridViewSmsComplaints.DataSource = dtRoom;
GridViewSmsComplaints.DataBind();
Response.Write("Done");
}
}
<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="6" runat="server" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" AutoGenerateColumns="False" CssClass="mGrid" BorderColor="#333333" Width="550px">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />
<%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" /> --%>
<asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" />
<asp:BoundField HeaderText="Message" DataField="Message" >
<ItemStyle Wrap="True" />
</asp:BoundField>
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:DropDownList ID="ddlValidity" runat="server">
<asp:ListItem Value="1" Text="Valid"></asp:ListItem>
<asp:ListItem Value="0" Text="Invalid"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
错误: 对象引用未设置为对象的实例。
第77行:dtRoom.Columns.Add(新的DataColumn(&#34; ID&#34;,typeof(Int64)));