我在gridview中有一个更新行的代码,但问题是,当我编辑和更新Row时,它给出了错误,因为输入字符串的格式不正确。在下面的代码行: -
cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Convert.ToInt32(Active);
请参阅我的代码供您参考: -
<asp:GridView ID="grdPostData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;"
CellPadding="3" AutoGenerateColumns="False" AllowPaging="true" PageSize="4" CssClass="hoverTable"
OnPageIndexChanging="grdPostData_PageIndexChanging" OnRowEditing="grdPostData_RowEditing"
OnRowDataBound="grdPostData_RowDataBound" DataKeyNames="Id" OnRowCommand="grdPostData_RowCommand" OnRowUpdating="grdPostData_RowUpdating">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" ItemStyle-Width="30" ControlStyle-CssClass="k-grid td">
<ControlStyle CssClass="k-grid td"></ControlStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="description" HeaderText="Description" ItemStyle-Width="30"
ControlStyle-CssClass="k-grid td">
<ControlStyle CssClass="k-grid td"></ControlStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Post Category" ItemStyle-Width="50">
<ItemTemplate>
<asp:DropDownList ID="ddlPostCategory" AppendDataBoundItems="true" runat="server"
AutoPostBack="false">
<%-- <asp:ListItem Text="Select" Value="0"></asp:ListItem>--%>
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Width="50px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" ControlStyle-CssClass="k-grid td">
<ControlStyle CssClass="k-grid td"></ControlStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:ImageButton ID="btnDelete" AlternateText="Delete" CommandName="eDelete" CommandArgument='<%# Bind("Id") %>' ImageUrl="~/images/delete.png"
runat="server" Width="15px" Height="15px" CausesValidation="False"
OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
<HeaderStyle Width="15%"></HeaderStyle>
</asp:TemplateField>
<asp:CommandField ButtonType="Image" ItemStyle-Width="15" EditImageUrl="~/images/edit.png" CausesValidation="false"
ShowEditButton="True" ControlStyle-Width="15" ControlStyle-Height="15" CancelImageUrl="~/images/close.png"
UpdateImageUrl="~/images/update.png">
<ControlStyle Height="20px" Width="20px"></ControlStyle>
<ItemStyle Width="15px"></ItemStyle>
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
No Result Found
</EmptyDataTemplate>
</asp:GridView>
另见CS代码: -
protected void grdPostData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool IsUpdated = false;
//getting key value, row id
int Id = Convert.ToInt32(grdPostData.DataKeys[e.RowIndex].Value.ToString());
//get all the row field detail here by replacing id's in FindControl("")..
GridViewRow row = grdPostData.Rows[e.RowIndex];
string PostTitle = ((TextBox)(row.Cells[0].Controls[0])).Text;
string Postdesc = ((TextBox)(row.Cells[1].Controls[0])).Text;
// string ddlPostCategory = ((DropDownList)(row.Cells[2].Controls[0])).SelectedValue;
string Active = ((TextBox)(row.Cells[3].Controls[0])).Text;
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE tbl_Post SET title=@title, description=@description, active=@active WHERE Id=@Id";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id;
cmd.Parameters.Add("@title", SqlDbType.NVarChar, 155).Value = PostTitle;
cmd.Parameters.Add("@description", SqlDbType.NVarChar, 99999999).Value = Postdesc;
cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Convert.ToInt32(Active);
cmd.Connection = conn;
conn.Open();
IsUpdated = cmd.ExecuteNonQuery() > 0;
conn.Close();
}
if (IsUpdated)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('page updated sucessfully');window.location ='csrposts.aspx';", true);
BindGrid();
}
else
{
//Error while updating details
grdPostData.EditIndex = -1;
//bind gridview here..
//GET GDATA FROM DATABASE AND BIND TO GRID VIEW
}
}
我知道这不值得一个问题,试过但找不到。请帮忙。
Dropdownlist aspx的代码: -
<div class="controls">
<asp:DropDownList ID="ddlActiveInactive" CssClass="selectpicker form-control-drp wd"
runat="server" Style="width: 100%" ValidationGroup="AddNew">
<asp:ListItem Value="1" Text="Active"></asp:ListItem>
<asp:ListItem Value="0" Text="InActive"></asp:ListItem>
</asp:DropDownList>
</div>
答案 0 :(得分:1)
这是一个下拉列表,其值为Active和InActive
如果您确实希望获得DropDownList
所选值,请使用FindControl
:
var ddlActive = (DropDownList)row.FindControl("ddlActiveInactive");
int active = -1; // or whatever
if(ddlActive.SelectedValue != "")
active = int.Parse(ddlActive.SelectedValue);
// ...
cmd.Parameters.Add("@Active", SqlDbType.Int)
.Value = active != -1 ? active : System.Data.SqlTypes.SqlInt32.Null;
答案 1 :(得分:0)
使用trim()
函数删除空格并将其转换为int
。
cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Convert.ToInt32(Active.Trim());