Hi, I'm new with asp.net and I need to use gridview but I'm not succeding with the rowupdating event.
My Code:
protected void POGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = null;
SqlCommand sqlComm = null;
SqlDataReader rdr = null;
/*DataTable dt = (DataTable)????;
GridViewRow row = POGridView.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Status"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem;
dt.Rows[row.DataItemIndex]["Priority"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
POGridView.EditIndex = -1;
BindGridView(ddlRoutes, new EventArgs());*/
GridView gv = (GridView)sender;
for (int i = 0; i < gv.Columns.Count; i++)
{
DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, false);
}
try
{
conn = new SqlConnection(Globals.conString);
conn.Open();
sqlComm = new SqlCommand("Update tbl_PO SET Status='" + e.NewValues[5].ToString () +
"', Priority=" + e.NewValues[6] +
" Where POID = '111'", conn);
rdr = sqlComm.ExecuteReader();
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
//e.Cancel = true;
//POGridView.EditIndex = -1;
//BindGridView(ddlRoutes, new EventArgs());
}Although I assigned false in :
gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, false); to avoid read only fields I'm not getting the values of non-read only ones that I had edited!!!
How does it work!
My Grid looks like that:
<asp:GridView ID="POGridView"
Width="80%"
AllowPaging="false"
PageSize="10"
AllowSorting="false"
DataKeyNames="POID"
HeaderStyle-BackColor="#00A3E4"
AlternatingRowStyle-BackColor="AliceBlue"
AutoGenerateEditButton="true"
EditRowStyle-BackColor="#D3EDF3"
AutoGenerateColumns="false"
AutoGenerateDeleteButton="false"
AutoGenerateSelectButton="false"
OnRowEditing="POGridView_RowEditing"
OnRowCancelingEdit ="POGridView_RowCancelingEdit"
OnRowUpdating="POGridView_RowUpdating"
OnPageIndexChanging="POGridView_PageIndexChanging"
OnSelectedIndexChanging="POGridView_SelectedIndexChanging"
Visible="true"
runat="server">
<Columns>
<asp:BoundField HeaderText="POUID" Visible ="false" DataField="POUID" />
<asp:BoundField HeaderText="POID" DataField="POID" ReadOnly="true" />
<asp:BoundField HeaderText="CatalogID" ReadOnly="true" DataField="CatalogID" />
<asp:BoundField HeaderText="CatalogDesc" ReadOnly="true" DataField="Description" />
<asp:BoundField HeaderText="RequestedQty" ReadOnly="true" DataField="RequestedQty" />
<asp:BoundField HeaderText="Status" ReadOnly="true" DataField="Status" Visible ="true" />
<asp:TemplateField HeaderText="Status" Visible ="false">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text='<%# Eval("Status") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server"
Width="100px"
DataSourceID="dsStatus"
DataTextField="StatusID"
DataValueField="StatusID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Priority" DataField="Priority" />
</Columns>
</asp:GridView>
Note: I faced a problem with saving the data of the status. So I made two columns, one to keep the value and one to change it later, I did that cause I'm filling the status list with values that I get from a stored procedure that get the old value as a parameter:
<asp:BoundField HeaderText="Status" ReadOnly="true" DataField="Status" Visible ="true" />
<asp:TemplateField HeaderText="Status" Visible ="false">
isn't there a better way to solve it?!
Best Regards.