本文介绍了网格视图中的行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<asp:GridView ID="grd1" runat="server" AutoGenerateColumns="false" Width="100%"
ForeColor="WhiteSmoke" DataKeyNames="ID" onrowediting="grd1_RowEditing"
onrowupdating="grd1_RowUpdating"
onrowcancelingedit="grd1_RowCancelingEdit"
onrowdatabound="grd1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID" ItemStyle-ForeColor="Aqua" HeaderStyle-ForeColor="IndianRed">
<ItemTemplate>
<%# Eval("ID") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paper Title" HeaderStyle-ForeColor="IndianRed">
<ItemTemplate>
<%# Eval("papertitle")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Author Name" HeaderStyle-ForeColor="IndianRed" >
<ItemTemplate>
<%# Eval("author") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Author's Email ID" HeaderStyle-ForeColor="IndianRed">
<ItemTemplate>
<%# Eval("email") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Journal" HeaderStyle-ForeColor="IndianRed">
<ItemTemplate>
<%# Eval("attachResearchPaper")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Download" HeaderStyle-ForeColor="IndianRed">
<ItemTemplate>
<asp:LinkButton ID="lnkdwn" runat="server" CommandName="download" CommandArgument='<%# Eval("FilePath") %>' Text="Download" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Auto Generated ID" HeaderStyle-ForeColor="IndianRed">
<ItemTemplate>
<%# Eval("autogenID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Admin Comment" Visible="true">
<ItemTemplate>
<%# Eval("Admin_Comment")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="t1" runat="server" Text='<%# Eval("Admin_Comment") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approved Or Rejected" Visible="true" >
<ItemTemplate>
<asp:Label runat="server" ID="LL0" Visible="true" Text='<%# Eval("ApprovedORrejected") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" ID="LL0" Visible="false" Text='<%# Eval("ApprovedORrejected") %>'></asp:Label>
<asp:DropDownList runat="server" ID="drpAOR_1">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assign_To" Visible="true" >
<ItemTemplate>
<asp:Label runat="server" ID="LL1" Visible="true" Text='<%# Eval("assign_TO") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" ID="LL1" Visible="false" Text='<%# Eval("assign_TO") %>'></asp:Label>
<asp:DropDownList runat="server" ID="drpAssign_to">
<asp:ListItem Selected="True" Text="Select" Value="Select">Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned On Date" Visible="true" >
<ItemTemplate>
<%# Eval("Assigned_On_Date")%>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="EDIT" CancelText="CANCEL" EditText="EDIT" ShowEditButton="true" ShowCancelButton="true" />
</Columns>
</asp:GridView>
protected void bind_grd1()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "select_grid_demo";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
grd1.DataSource = ds;
grd1.DataBind();
}
protected void grd1_RowEditing(object sender, GridViewEditEventArgs e)
{
grd1.EditIndex = e.NewEditIndex;
bind_grd1();
}
protected void grd1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string admn_cmmnt=(grd1.Rows[e.RowIndex].FindControl("t1") as TextBox).Text;
string assgnTO = (grd1.Rows[e.RowIndex].FindControl("drpAssign_to") as DropDownList).SelectedItem.Value;
string aprORrjct = (grd1.Rows[e.RowIndex].FindControl("drpAOR_1") as DropDownList).SelectedItem.Value;
string j_ID = grd1.DataKeys[e.RowIndex].Value.ToString();
string strConnString = ConfigurationManager.AppSettings["constring"];
//string strConnString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con1 = new SqlConnection(strConnString))
{
string query1 = "update Admin_Added_Details set Admin_Comment=@Admin_Comment1,A_Assigned_To=@A_Assigned_To1,ApprovedORrejected=@ApprovedORrejected1 where ID=@ID1";
using (SqlCommand cmd2 = new SqlCommand(query1))
{
cmd2.Connection = con1;
cmd2.Parameters.AddWithValue("@Admin_Comment1", admn_cmmnt);
cmd2.Parameters.AddWithValue("@A_Assigned_To1", assgnTO);
cmd2.Parameters.AddWithValue("@ApprovedORrejected1",aprORrjct);
cmd2.Parameters.AddWithValue("@ID1",j_ID);
con1.Open();
cmd2.ExecuteNonQuery();
con1.Close();
}
}
grd1.EditIndex = -1;
bind_grd1();
}
protected void grd1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grd1.EditIndex = -1;
bind_grd1();
}
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && grd1.EditIndex==e.Row.RowIndex)
{
DropDownList ddrrpp = (DropDownList)e.Row.FindControl("drpAOR_1");
string query = "select ApprovedORrejected from approved_OR_rejected ";
SqlCommand cmd2 = new SqlCommand(query);
ddrrpp.DataSource = GetData(cmd2);
ddrrpp.DataTextField = "ApprovedORrejected";
ddrrpp.DataValueField = "ApprovedORrejected";
ddrrpp.DataBind();
Label lb = (e.Row.FindControl("LL0") as Label);
if (lb != null)
{
ListItem dd = ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text);
if (dd != null)
{
dd.Selected = true;
}
}
//ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text).Selected = true;
}
if (e.Row.RowType == DataControlRowType.DataRow && grd1.EditIndex == e.Row.RowIndex)
{
DropDownList ddrrpp1 = (DropDownList)e.Row.FindControl("drpAssign_to");
string query = "select U_Name from Add_New_User ";
SqlCommand cmd3 = new SqlCommand(query);
ddrrpp1.DataSource = GetData(cmd3);
ddrrpp1.DataTextField = "U_Name";
ddrrpp1.DataValueField = "U_Name";
ddrrpp1.DataBind();
Label lb = (e.Row.FindControl("LL1") as Label);
if (lb != null)
{
ListItem dd1 = ddrrpp1.Items.FindByValue((e.Row.FindControl("LL1") as Label).Text);
if (dd1 != null)
{
dd1.Selected = true;
}
}
//ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text).Selected = true;
}
}
private DataTable GetData(SqlCommand cmd2)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
//string strr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
//using (SqlConnection conn = new SqlConnection(strr))
//{
using (SqlDataAdapter ssda = new SqlDataAdapter())
{
cmd2.Connection = con;
ssda.SelectCommand = cmd2;
using (DataTable ddt = new DataTable())
{
ssda.Fill(ddt);
return ddt;
}
//}
}
}
while row updating, cant update column \"assign_TO\"...
Please help..
while row updating, cant update column "assign_TO"...
Please help..
推荐答案
这篇关于网格视图中的行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!