小さな問題私のコードでは解決できません。私は新しい空の行を追加する必要があるImageButtonを持つGridViewを持っています。私はこのエラーを取得するいくつかの理由:C#GridViewに空行を追加
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] CTI_DFC.Default.gv_Steps_RowCommand(Object sender, GridViewCommandEventArgs e) +469
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +172
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +163 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +83
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3771
だから、これは私が持っているGridViewコントロールである:
<asp:GridView ID="gv_Steps" runat="server" CssClass="lbl_user" Font-Names="Verdana" Font-Size="10px" HeaderStyle-Height="23.5px" HorizontalAlign="Center"
RowStyle-Height="23.5px" ShowHeaderWhenEmpty="True" style="z-index: 1; left: 8px; top: 143px; position: absolute; height: 20px; width: 1178px;" AutoGenerateColumns="False" OnRowCommand="gv_Steps_RowCommand">
<AlternatingRowStyle BackColor="#DCE4FF" />
<Columns>
<asp:TemplateField HeaderText="Step" HeaderStyle-HorizontalAlign="Left" >
<ItemTemplate>
<asp:TextBox ID="txt_Step" HeaderStyle-HorizontalAlign="Left" runat="server" Width="150px" Text='<%# Bind("Step") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Server" HeaderStyle-HorizontalAlign="Left" >
<ItemTemplate>
<asp:TextBox ID="txt_Server" HeaderStyle-HorizontalAlign="Left" runat="server" Width="150px" Text='<%# Bind("Server") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type" HeaderStyle-HorizontalAlign="Left" >
<ItemTemplate>
<asp:TextBox ID="txt_Type" HeaderStyle-HorizontalAlign="Left" runat="server" Width="150px" Text='<%# Bind("Type") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job" HeaderStyle-HorizontalAlign="Left" >
<ItemTemplate>
<asp:TextBox ID="txt_Job" HeaderStyle-HorizontalAlign="Left" runat="server" Width="150px" Text='<%# Bind("Job") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number" HeaderStyle-HorizontalAlign="Left" >
<ItemTemplate>
<asp:TextBox ID="txt_Number" HeaderStyle-HorizontalAlign="Left" runat="server" Width="150px" Text='<%# Bind("Number") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="15px" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:ImageButton ID="btn_AddRow" runat="server" AlternateText="Add Row" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Add" ImageUrl="./Img/ADD.png" ToolTip="Add Row without Change" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="15px" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:ImageButton ID="btn_DeleteRow" runat="server" AlternateText="Delete Row" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Delete" ImageUrl="./Img/DEL.png" ToolTip="Delete Row without Change" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#0B4DA2" Font-Bold="True" ForeColor="White" />
<RowStyle Height="23px" />
</asp:GridView>
そして、これは私がそれをやろうとしているどのようにC#のコードです:
protected void gv_Steps_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intIndex = Convert.ToInt32(e.CommandArgument); //will be used for Delete
if (e.CommandName == "Delete" && (gv_Steps.Rows.Count > 1))
{
//Nothing done yet
}
if (e.CommandName == "Add")
{
DataTable dt = gv_Steps.DataSource as DataTable;
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
gv_Steps.DataSource = dt;
gv_Steps.DataBind();
}
}
アドバイスありがとうございます!
EDIT
私は私のDataTableが空であり、そのように動作しないことを今理解が、どのように私は新しい空の行で戻ってDataSetにGridViewコントロールを取得し、できますか?私は同じエラーでこのようにそれを試してみました:
protected void gv_Steps_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intIndex = Convert.ToInt32(e.CommandArgument); //will be used for Delete
if (e.CommandName == "Delete" && (gv_Steps.Rows.Count > 1))
{
//Nothing done yet
}
if (e.CommandName == "Add")
{
DataTable dt = GridviewToDataTable();
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
gv_Steps.DataSource = dt;
gv_Steps.DataBind();
}
}
private DataTable GridviewToDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Step", typeof(string)));
dt.Columns.Add(new DataColumn("Server", typeof(string)));
dt.Columns.Add(new DataColumn("Type", typeof(string)));
dt.Columns.Add(new DataColumn("Job", typeof(string)));
dt.Columns.Add(new DataColumn("Number", typeof(string)));
foreach (GridViewRow row in gv_Steps.Rows)
{
dt.Rows.Add();
dt.Rows[row.RowIndex][0] = ((TextBox)row.FindControl("Step")).Text;
dt.Rows[row.RowIndex][1] = ((TextBox)row.FindControl("Server")).Text;
dt.Rows[row.RowIndex][2] = ((TextBox)row.FindControl("Type")).Text;
dt.Rows[row.RowIndex][3] = ((TextBox)row.FindControl("Job")).Text;
dt.Rows[row.RowIndex][4] = ((TextBox)row.FindControl("Number")).Text;
}
return dt;
}
私が取得エラー:私は取得
protected void gv_Steps_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intIndex = Convert.ToInt32(e.CommandArgument); //will be used for Delete
if (e.CommandName == "Delete" && (gv_Steps.Rows.Count > 1))
{
//Nothing done yet
}
if (e.CommandName == "Add")
{
DataTable dt = GridviewToDataTable(gv_Steps);
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
gv_Steps.DataSource = dt;
gv_Steps.DataBind();
GridviewToDataTable(gv_Steps);
}
}
private DataTable GridviewToDataTable(GridView gv)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Step", typeof(string)));
dt.Columns.Add(new DataColumn("Server", typeof(string)));
dt.Columns.Add(new DataColumn("Type", typeof(string)));
dt.Columns.Add(new DataColumn("Job", typeof(string)));
dt.Columns.Add(new DataColumn("Number", typeof(string)));
foreach (GridViewRow row in gv.Rows)
{
dt.Rows.Add();
dt.Rows[row.RowIndex][0] = ((TextBox)row.FindControl("Step")).Text;
dt.Rows[row.RowIndex][1] = ((TextBox)row.FindControl("Server")).Text;
dt.Rows[row.RowIndex][2] = ((TextBox)row.FindControl("Type")).Text;
dt.Rows[row.RowIndex][3] = ((TextBox)row.FindControl("Job")).Text;
dt.Rows[row.RowIndex][4] = ((TextBox)row.FindControl("Number")).Text;
}
gv.DataSource = dt;
gv.DataBind();
return dt;
}
エラー:
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] CTI_DFC.Default.GridviewToDataTable() +1161
CTI_DFC.Default.gv_Steps_RowCommand(Object sender, GridViewCommandEventArgs e) +392
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +172
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +163 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +83
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3771
EDIT 2
はこのようにそれを試してみました:
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] CTI_DFC.Default.GridviewToDataTable(GridView gv) +1145
CTI_DFC.Default.gv_Steps_RowCommand(Object sender, GridViewCommandEventArgs e) +407
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +172
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +163 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +83
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3771
他のアドバイスはありますか?
SOLUTION
は、私はそれをしなかった、それはこのように行われました:
private DataTable GridviewToDataTable(GridView gv)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Step", typeof(string)));
dt.Columns.Add(new DataColumn("Server", typeof(string)));
dt.Columns.Add(new DataColumn("Type", typeof(string)));
dt.Columns.Add(new DataColumn("Job", typeof(string)));
dt.Columns.Add(new DataColumn("Number", typeof(string)));
foreach (GridViewRow row in gv.Rows)
{
dt.Rows.Add();
dt.Rows[row.RowIndex][0] = (row.FindControl("txt_Step") as TextBox).Text;
dt.Rows[row.RowIndex][1] = (row.FindControl("txt_Server") as TextBox).Text;
dt.Rows[row.RowIndex][2] = (row.FindControl("txt_Type") as TextBox).Text;
dt.Rows[row.RowIndex][3] = (row.FindControl("txt_Job") as TextBox).Text;
dt.Rows[row.RowIndex][4] = (row.FindControl("txt_Number") as TextBox).Text;
}
lbl_Fehlermeldung.Text = dt.Rows[0][0].ToString();
lbl_Fehlermeldung.Visible = true;
gv.DataSource = dt;
gv.DataBind();
return dt;
}
'gv_Steps.DataSource'しかないポストバックに、初期負荷で動作します。 – VDWWD