Two gridview's selected indexchanged event does not work - c#

I set two Gridview in one page. one's selected index changed event's load another event. But 2nd grid's selectedindexchanged event goes on first's selectedindexchanged event. What can I do? My function name was correct. Not conflicting any other.
Here my asp code :
<asp:GridView ID="gdv" runat="server"
AutoGenerateColumns="False" onrowdatabound="gdv_RowDataBound"
onselectedindexchanged="gdv_SelectedIndexChanged" AllowPaging="True"
Width="100%" onpageindexchanging="gdv_PageIndexChanging">
<Columns>
<asp:BoundField DataField="Sl" HeaderStyle-HorizontalAlign="Center"
HeaderText="SL #" ItemStyle-HorizontalAlign="Center">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="SalesOrderNo" HeaderStyle-HorizontalAlign="Left"
HeaderText="Order No" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SectionID" HeaderStyle-HorizontalAlign="Left"
HeaderText="Section Id" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SectionName" HeaderStyle-HorizontalAlign="Left"
HeaderText="Section Name" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="OutletID" HeaderStyle-HorizontalAlign="Left"
HeaderText="Outlet Id" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="OutletName" HeaderStyle-HorizontalAlign="Left"
HeaderText="outlet Name" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SalesPersonID" HeaderStyle-HorizontalAlign="Left"
HeaderText="Sales Person Name" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="OrderDate" HeaderStyle-HorizontalAlign="Left"
HeaderText="Order Date" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="OrderValue" HeaderStyle-HorizontalAlign="Left"
HeaderText="Order Value" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="checkOrderList" runat="server" ValidationGroup="ValCheckOrder" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%--<asp:LinkButton ID="btnView" Text="View" runat="server" CommandName="ViewIndividualOrder" />--%>
<asp:Button ID="btnViewOrder" runat="server" Text="View" ValidationGroup="ValCheckOrder"
CommandName="ViewIndividualOrder" OnClick="btnViewOrder_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
<asp:GridView ID="gdvOrderDetail" runat="server"
AutoGenerateColumns="False" onrowdatabound="gdvOrderDetail_RowDataBound"
onselectedindexchanged="gdvOrderDetail_SelectedIndexChanged" AllowPaging="True"
Width="100%" onpageindexchanging="gdvOrderDetail_PageIndexChanging">
<Columns>
<asp:BoundField DataField="Sl" HeaderStyle-HorizontalAlign="Center"
HeaderText="SL #" ItemStyle-HorizontalAlign="Center">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ProductID" HeaderStyle-HorizontalAlign="Left"
HeaderText="Product ID" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderStyle-HorizontalAlign="Left"
HeaderText="Product Name" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="UnitID" HeaderStyle-HorizontalAlign="Left"
HeaderText="Unit" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="PacketSize" HeaderStyle-HorizontalAlign="Left"
HeaderText="Packet Size" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Sales Rate" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblSalesRate" runat="server" Text='<%# Eval("SalesRate") %>' Font-Size="Smaller" />
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="SalesRate" HeaderStyle-HorizontalAlign="Left"
HeaderText="Sales Rate" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>--%>
<asp:BoundField DataField="OrderQty" HeaderStyle-HorizontalAlign="Left"
HeaderText="Order Qty" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="MemoQty" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="txtMemoQty" runat="server" Font-Bold="True" Width="100px" OnTextChanged="txtMemoQty_TextChanged"
AutoPostBack="true" Enabled="true" onkeypress="return onlyNumbers();"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorMemoQTY" ValidationGroup="ValCheckOrderDetail"
runat="server" ControlToValidate="txtMemoQty" ErrorMessage="*" Font-Size="X-Large"
ForeColor="Red"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text='<%# Eval("Amount") %>' Font-Size="Smaller" />
</ItemTemplate>
</asp:TemplateField>
<%--<asp:BoundField DataField="Amount" HeaderStyle-HorizontalAlign="Left"
HeaderText="Amount" ItemStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>--%>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="checkOrderList" runat="server" ValidationGroup="ValCheckOrderDetail" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
C# code:
protected void gdv_SelectedIndexChanged(object sender, EventArgs e)
{
int indx = 0;
indx = gdv.SelectedIndex;
string salesorderno = "";
string sectionid = "";
string sectionname = "";
string outletid = "";
string outletname = "";
string salespersonname = "";
string orderdate = "";
string ordervalue = "";
string ordercheck = "";
for (int i = 0; i < 1; i++)
{
salesorderno = gdv.Rows[indx].Cells[1].Text.ToString();
sectionid = gdv.Rows[indx].Cells[2].Text.ToString();
sectionname = gdv.Rows[indx].Cells[3].Text.ToString();
outletid = gdv.Rows[indx].Cells[4].Text.ToString();
outletname = gdv.Rows[indx].Cells[5].Text.ToString();
salespersonname = gdv.Rows[indx].Cells[6].Text.ToString();
orderdate = gdv.Rows[indx].Cells[7].Text.ToString();
ordervalue = gdv.Rows[indx].Cells[8].Text.ToString();
ordercheck = gdv.Rows[indx].Cells[9].Text.ToString();
}
lblOrderNo.Text = salesorderno;
lblSectionName.Text = sectionname;
lblOutletName.Text = outletname;
lblOrderDate.Text = orderdate;
LoadOrderReportDetailGrid(salesorderno);
}
protected void gdvOrderDetail_SelectedIndexChanged(object sender, EventArgs e)
{
}

test is sufficient to know the origin of the event.
Exemple :
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(dataGrid1.SelectedItem!=null)
MessageBox.Show("aaaaaaaaaaa");
}
private void dataGrid2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (dataGrid2.SelectedItem != null)
MessageBox.Show("BBBBBbbBBBB ");
}

Just open the event window for the gridviews then select one of them and on the event window double click on the "selected index changed" event. it will move you to the code section. if you are in true procedure than the event binding is true. if not you have to change the definition of your procedure.
Here is the simple process.
copy the codes inside the procedure and paste them to a safe place.
delete the procedures related the gridviews
check the event page for gridviews to ensure there is nothing in the "selected index changed" event.
double click the first gridviews "selected index changed" event. it will create a blank procedure. paste the related code here.
do the same for second.
and the other thing check the validation groups of the gridviews in the properties window. they should be different :)
NOT: forgive me that i cannot write the code directly. because i am not with my own computer. I hope it works.

Related

how to get cell value from gridview

protected void CovidSelfDecleartionDeatils_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
ShowallRecord();
if (e.CommandArgument != "")
{
int Index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName=="Select")
{
DataTable dt_main = new DataTable();
dt_main = (DataTable)ViewState["ReportData"];
GridViewRow clickedRow = CovidSelfDecleartionDeatils.Rows[Index];
Session["Filepath"] = clickedRow.Cells[9].Text;
//Session["Filepath"] = CovidSelfDecleartionDeatils.Rows[Index].FindControl("FilePath");
Session["Filepath1"] = CovidSelfDecleartionDeatils.DataKeys[Index]["Filepath"].ToString();
string filepath = Session["Filepath"].ToString();
//DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
//string val = (string)rows[2].Cells["Late_Time"].Value;
}
}
}
catch
{
}
}
<asp:GridView ID="CovidSelfDecleartionDeatils" runat="server" DataKeyNames="EmployeeCode" AutoGenerateColumns="False"
BackColor="#333333" CellPadding="5" CellSpacing="3" ForeColor="Black" Width="100%" Height="300px" OnRowCommand="CovidSelfDecleartionDeatils_RowCommand" >
<Columns>
<%-- <asp:BoundField DataField="InitiateMPPId" HeaderText="InitiateMPPId" >
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>--%>
<asp:BoundField DataField="EmployeeCode" HeaderText="EmployeeCode" ItemStyle-HorizontalAlign="Left" Visible="false"
HeaderStyle-HorizontalAlign="Left" ControlStyle-BorderStyle="None" ItemStyle-CssClass="per10"
HeaderStyle-CssClass="header-center">
</asp:BoundField>
<%-- <asp:BoundField DataField="EmployeeCode" HeaderText="EmployeeCode" ItemStyle-HorizontalAlign="Left" Visible="false"
HeaderStyle-HorizontalAlign="Left" ControlStyle-BorderStyle="None" ItemStyle-CssClass="per10"
HeaderStyle-CssClass="header-center">
</asp:BoundField>--%>
<asp:BoundField DataField="EmpName" HeaderText="Employee" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" >
</asp:BoundField>
<asp:BoundField DataField="Decleartion_Date" HeaderText="Date Decleartion" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:dd-MM-yyyy}" >
</asp:BoundField>
<asp:BoundField DataField="Ferver" HeaderText="Ferver" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Cough" HeaderText="Cough" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="Breathlessness" HeaderText="Breathlessness" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="OtherDiseases" HeaderText="OtherDiseases" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true"/>
<asp:BoundField DataField="FileName" HeaderText="FileName" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true" Visible="false" />
<asp:BoundField DataField="DeclarationID" HeaderText="DeclarationID" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true" Visible="false" />
<asp:BoundField DataField="FilePath" HeaderText="FilePathfordownload" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true" Visible="false" />
<asp:BoundField DataField="ContactwithCovid" HeaderText="Incontact with covid patient Last(14) days" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-Width="15%" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true"/>
<%--<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btn_delete_record"
Width="20px" Height="20px" runat="server" ImageUrl="~/Content/Images/NewIcons/PDF.png" />
</ItemTemplate>
</asp:TemplateField>--%>
<asp:ButtonField CommandName="Select" ControlStyle-CssClass="button" ButtonType="Image" ImageUrl="~/Content/Images/NewIcons/PDF.png"
ItemStyle-Width="5px" HeaderText="Download Attachment" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="TravelDate" HeaderText="TravelDate" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:dd-MM-yyyy}" />
<asp:BoundField DataField="StateName" HeaderText="StateName" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="#5e5d5d" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="Gray" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
what wrong in my showing excetion " Object reference is not set" when i am saving value in session
remove visible=false and define new css class like
.donotshow { display: none; }
and add it HeaderStyle-CssClass and ItemStyle-CssClass

Save CheckBox Values in GridView to Database. ASP.NET

i am developing webform with gridview. I have a problem, the value of checkbox doesn't save in the table. I've tried everything nothing is working. After i press a button, value doesn't store.
And as you can see in my code, i don't use id for unidentified of each particular row, instead id i use foreach
protected void Button_Submit_Onclick(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView2.Rows)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["TestDeductionsConnectionString2"].ToString();
bool private1 = (row.FindControl("CheckBox1") as CheckBox).Checked;
SqlCommand cmd = new SqlCommand("insert into DetailCosts(private) values(#private1)", con);
if (private1 == true)
{
cmd.Parameters.Add(new SqlParameter("#private1", SqlDbType.Bit));
cmd.Parameters["#private1"].Value = 1;
//cmd.Parameters.Add("#private1", SqlDbType.Bit).Value = 1;
}
else
{
cmd.Parameters.Add(new SqlParameter("#private1", SqlDbType.Bit));
cmd.Parameters["#private1"].Value = 0;
//cmd.Parameters.Add("#private1", SqlDbType.Bit).Value = 0;
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}}
and there is a code with my Gridview:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" DataKeyNames="recordid">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="recordid" HeaderText="recordid" SortExpression="recordid">
</asp:BoundField>
<asp:BoundField DataField="Employee" HeaderText="Employee" SortExpression="Employee">
</asp:BoundField>
<asp:BoundField DataField="mesec" HeaderText="Month" SortExpression="mesec">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="godina" HeaderText="Year" SortExpression="godina">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="CallDate" HeaderText="Call Date" SortExpression="CallDate">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="PhoneType" HeaderText="Phone Type" SortExpression="PhoneType">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="land" HeaderText="Land" SortExpression="land">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="number" HeaderText="Number" SortExpression="number">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Minutes" HeaderText="Minutes" SortExpression="Minutes">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Costs" HeaderText="Costs" SortExpression="Costs">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="privateCost" HeaderText="Private Costs" SortExpression="privateCost">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Private" SortExpression="private" >
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("private") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("private") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Komentar" HeaderText="Commnet" SortExpression="Komentar">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>

Update GridViewRow on button click of that row

I have a GridView with checkbox, dropdown and button as templatefields.
On button click I have to update the entire row in the data base.
This is my aspx:
<asp:GridView runat="server" ID="Gdv" AutoGenerateColumns="False" Font-Size="Small" CssClass="grid" BackColor="White" BorderWidth="0px" CellPadding="4" Width="100%" AllowSorting="True" SkinID="GVSalesManager" GridLines="none" AllowPaging="true" PageSize="10" PagerStyle-ForeColor="#0066cc" PagerStyle-CssClass="gvPagerCss" PagerStyle-Font-Underline="true" PagerStyle-Wrap="true"
OnPageIndexChanging="GdvCPRetailerMap_PageIndexChanging" OnRowDataBound="GdvCPRetailerMap_RowDataBound">
<Columns>
<asp:BoundField ReadOnly="true" HeaderText="S.No" DataField="S.No." SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="2%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="2%"/>
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="chkRow_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ReadOnly="true" HeaderText="Customer Id" ItemStyle-HorizontalAlign="Center" DataField="CustomerID" SortExpression="CustomerID">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="10%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Firm's Name" ItemStyle-HorizontalAlign="Center" DataField="Firm's Name" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="20%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="20%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Retailer Name" ItemStyle-HorizontalAlign="Center" DataField="Retialer Name" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="20%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="20%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Pesticide Licence No." ItemStyle-HorizontalAlign="Center" DataField="Pesticide Licence No" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="10%"/>
</asp:BoundField>
<asp:TemplateField HeaderText="Channel Partner-1" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlCP1" Enabled="false" ToolTip="Please Click on the checkbox to change the prefered CP" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP1_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Channel Partner-2" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlCP2" Enabled="false" ToolTip="Please Click on the checkbox to change the prefered CP" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP2_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button runat="server" ID="BtnUpdateRow" Text="Update" ToolTip="Update This Record?" OnClick="BtnUpdateRow_Click" BackColor="#336699" ForeColor="#ffffff"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the updatebutton event
protected void BtnUpdateRow_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow grow = (GridViewRow)btn.NamingContainer;
//btn.
CheckBox chkbx = (CheckBox)grow.FindControl("chkRow");
if (chkbx.Checked != true)
{
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Red;
labelErrormessage.Text = "Please Check/Uncheck the CheckBox and Click Update";
return;
}
else
{
/*--Code to update the row's record in the edatabase.--*/
??
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Green;
labelErrormessage.Text = "Updated Succesfully";
return;
}
}
How do I read the row in which the button has been clicked and update its dropdownlist selected values in the database?
If you have some sort of identification data displayed in the gridview you could get the value from the row's cell like this grow.Cells[0].Text then find the record in the database and update its values.
You could also hide the id for example if this is information you dont want to show to the user. The row will still be located at index 0 but hidden.
Sample:
else
{
var record = queryDatabase(grow.Cells[0].Text);
//update record values
updateRecord(record);
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Green;
labelErrormessage.Text = "Updated Succesfully";
return;
}

Reading bounded DataItem expression text from control inside template field

I have a custom gridview and I use that grid in too many pages and I want to set the SortExpression text for all columns inside that grid in code behained ; as you see in the following code I have do that by overriding the OnRowDataBound eventhandler .
The problem is I can't access the evaluated datafield expression text ("START_WORK_DATE" in my case) for template Fields like what I did in BoundField
<eska:GridView ID="gvEmployees" runat="server" CssClass="new-grid" PageSize="20"
AllowSorting="true" AllowPaging="True"
AutoGenerateColumns="False" Width="100%"
GridContainer="tdEmployees" DataKeyNames="ID"
RowHoverCssClass="HoverRow" ShowCheckBox="True"
OnPageIndexChanging="gvEmployees_PageIndexChanging"
OnSelectedIndexChanged="gvEmployees_SelectedIndexChanged"
meta:resourcekey="gvEmployeesResource1">
<PagerSettings FirstPageImageUrl="~/App_Themes/Images/First.gif" LastPageImageUrl="~/App_Themes/Images/last.gif"
Mode="NextPreviousFirstLast" NextPageImageUrl="~/App_Themes/Images/Next.gif"
PreviousPageImageUrl="~/App_Themes/Images/prev.gif"></PagerSettings>
<SelectedRowStyle CssClass="SelectedRow"></SelectedRowStyle>
<Columns>
<asp:BoundField meta:resourcekey="BoundFieldResource1">
<ItemStyle Width="2%" />
<HeaderStyle Width="2%" />
<FooterStyle Width="2%" />
</asp:BoundField>
<asp:BoundField DataField="EMP_NO" HeaderText="Employee No"
meta:resourcekey="BoundFieldResource3">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="NAME" HeaderText="Name" meta:resourcekey="BoundFieldResource4">
<HeaderStyle Width="34%"></HeaderStyle>
<ItemStyle Width="34%"></ItemStyle>
<FooterStyle Width="34%" />
</asp:BoundField>
<asp:TemplateField HeaderText="Hiring Date" meta:resourcekey="TemplateFieldResource1">
<ItemTemplate>
<asp:Label runat="server"Text='<%# Convert.ToDateTime(DataBinder.Eval(Container,
"DataItem.START_WORK_DATE")).ToString("dd-MM-yyyy") %>'
ID="lblHiringDateGrid"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:TemplateField>
<asp:BoundField DataField="PHONE1" HeaderText="Phone 1"
meta:resourcekey="BoundFieldResource5">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="Position_Name" HeaderText="Position"
meta:resourcekey="BoundFieldResource6">
<HeaderStyle Width="20%"></HeaderStyle>
<ItemStyle Width="20%"></ItemStyle>
<FooterStyle Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="STATUS_DESC" HeaderText="Status"
meta:resourcekey="BoundFieldResource7">
<HeaderStyle Width="12%"></HeaderStyle>
<ItemStyle Width="12%"></ItemStyle>
<FooterStyle Width="12%" />
</asp:BoundField>
<asp:BoundField meta:resourcekey="BoundFieldResource8">
<ItemStyle Width="2%" />
<HeaderStyle Width="2%" />
<FooterStyle Width="2%" />
</asp:BoundField>
</Columns>
</eska:GridView>
Code behind:
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < this.Columns.Count; i++)
{
if (this.Columns[i] is BoundField)
{
if (((BoundField)(this.Columns[i])).DataField != string.Empty)
{
this.Columns[i].SortExpression = ((BoundField)(this.Columns[i])).DataField;
}
}
else if (this.Columns[i] is TemplateField)
{
// ???
}
}
}
}
You can do some thing like this. Set a header template with link button.
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="LinkButtonEmpName" runat="server" Text="Employee Name" CommandName="Sort" CommandArgument="Employees">
</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="LabelEmployee" runat="server" Text='<%# Bind("Employees") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxEmployee" runat="server"/>
</FooterTemplate>
And in row command event find command like this.
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName.Equals("Sort"))
{
FilterExpression = e.CommandArgument.ToString() + " LIKE '%" + txtCaseNumber.Text + "%'";
BindGridView();
}}
See the complete example here.
http://www.codeproject.com/Questions/528664/HowplustoplusapplyplussortingplusinplusTemplateplu

GridView affected by window.open

I have a gridView which works nicely until I open a new window by clicking a button. When the new window is opened the values in one of the columns 'lblTotalPrice' gets erased. 'lblTotalPrice' is a template field, which is calculated in MealList_RowDataBound (quantity * price). The footer for the column, however is not affected when the new window is opened (also gets its value in MealList_RowDataBound).
Also the font-size of the data-bound columns are changed when the new window is opened.
Any ideas of what might cause this? When stepping through the code there are no other lines executed after btnEnvComment_Click
When gridView is databound again, everything is back to normal.
The gridView:
<rwg:BulkEditGridView ID="MealList" runat="server" AutoGenerateColumns="False" ShowFooter="True" GridLines="Vertical" CellPadding="4"
BackColor="White" DataKeyNames="ItemId,ProductId" SaveButtonID="SaveButton"
DataSourceID="SqlMealItems" style="float:left"
OnRowDataBound="MealList_RowDataBound" OnRowCreated="MealList_RowCreated" CssClass="gridView">
<AlternatingRowStyle CssClass="MealListItemAlt" BackColor="#a6dbed" />
<Columns>
<asp:BoundField DataField="ProductId" HeaderText="Prod #" HeaderStyle-Wrap="false" ReadOnly="True" >
<HeaderStyle Wrap="False" Width="60px"></HeaderStyle>
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderText="Namn" ReadOnly="True" ItemStyle-Wrap="False" >
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Antal" ControlStyle-CssClass="numeric" >
<ControlStyle CssClass="numeric" Width="30px"></ControlStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Enhet">
<ItemTemplate>
<asp:DropDownList ID="DropDownUnit" OnSelectedIndexChanged="DropDownUnit_SelectedIndexChanged"
AutoPostBack="False" runat="server" DataTextField="Unit" DataValueField="Unit">
</asp:DropDownList>
</ItemTemplate>
<ControlStyle Width="70px" />
</asp:TemplateField>
<asp:BoundField HeaderText="รก pris" ReadOnly="True" ControlStyle-CssClass="rightAlign" ItemStyle-Wrap="False">
<ControlStyle CssClass="numeric" ></ControlStyle>
<ItemStyle HorizontalAlign="Right" Width="80px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Totalt" ItemStyle-Wrap="False">
<ItemTemplate>
<asp:label ID="lblTotalPrice" runat = server></asp:label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="80px" />
<FooterTemplate>
<asp:Label ID="lblTotalPricePerMeal" runat="server"></asp:Label>
</FooterTemplate>
<FooterStyle HorizontalAlign="Right" Font-Bold="true" Font-Overline="True" Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="Unit" HeaderText="h" ReadOnly="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:image runat="server" ID="imgBlob" Width="24"></asp:image>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblError"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle CssClass="MealListFooter" />
<HeaderStyle CssClass="MealListHead" />
</rwg:BulkEditGridView>
The code for the button that open the new window:
protected void btnEnvComment_Click(object sender, EventArgs e)
{
int offsetPos = 125; // set placement of new window
String URL = "EnvComment.aspx?MealID=" + Session["MealId"];
String responsCommand = "window.open('" + URL + "','_blank','height=400,width=300,top=" + ",left=" + offsetPos.ToString() + offsetPos.ToString() + ",titlebar=1')";
Response.Write("<script>");
Response.Write(responsCommand);
Response.Write("</script>");
}

Categories