i am filling database from store procedure but it doesn't work, even debugger doesn't hit. What is the problem, why it is blank ? I have tried several times with nd without debugging but it doesn't work man.
protected void GridViewConductorsDevices_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
DataTable ConductorDevicesInfo = new DataTable();
int ID = Convert.ToInt32(GridViewConductorsDevices.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
ViewState["ID"] = ID;
ConductorDevicesInfo = (DataTable)ManageTransport.ManageConductorDevices.GetAssignedDevices();
this.TextBoxID.Text = GridViewConductorsDevices.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
this.DropDownListConductors.SelectedValue= ConductorDevicesInfo.Rows[0]["Conductor_ID"].ToString();
this.DropDownListDevices.SelectedValue = ConductorDevicesInfo.Rows[0]["Device_ID"].ToString();
MultiView1.ActiveViewIndex = -1;
MultiView1.ActiveViewIndex = 2;
}
catch (Exception)
{
}
finally
{
}
}
.aspx
<asp:GridView ID="GridViewConductorsDevices" runat="server" AutoGenerateColumns="False" CaptionAlign="Top"
CssClass="table table-hover table-striped table-bordered" DataKeyNames="ID" OnRowCommand="GridViewConductorsDevices_RowCommand"
Width="100%">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Conductor_ID" Visible="false" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Device_ID" Visible="false" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Conductor" HeaderText="Conductor"
ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Terminal" HeaderText="Terminal" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- <asp:BoundField DataField="TotalDistance" HeaderText="Total Distance" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>--%>
<%-- <asp:BoundField DataField="Model" HeaderText="Model" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>--%>
<%--<asp:BoundField DataField="ChassisNo" HeaderText="Chassis No" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>--%>
<%--<asp:BoundField DataField="EngineNo" HeaderText="EngineNo" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>--%>
<asp:ButtonField Text="Edit" HeaderText="Option" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop"></asp:ButtonField>
</Columns>
</asp:GridView>
Your Gridview, <asp:GridView ID="GridViewConductorsDevices" runat="server" > needs a datasource.
You mention a stored procedure, but it's not referenced in your posted code.
To bind data to a Gridview you need to either assign one to the GridView DataSource property in the code behind followed by a call to GridViewConductorsDevices.DataBind()
Alternatively you can provide the ID of a DataSource control to the Gridview DataSourceID property which will bind automatically during the Page Lifecycle.
However in either case, the event GridViewConductorsDevices_RowCommand will never get called as you have not defined a CommandField to trigger a row event.
Related
I have a problem that is most likely easy but I'm missing something small.
I have looked at numerous entries on Stack Overflow with the same question but I must be doing something wrong.
I am trying to get the value of a textbox in a GridView after typing into it. I want to take that value and update a SQL field.
Here is my GridView:
<asp:GridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SALESID" HeaderStyle-HorizontalAlign="CENTER" HorizontalAlign="CENTER" CellPadding="4" ForeColor="#333333" GridLines="Both" AllowSorting="true" AllowPaging="true" PageSize="100" OnRowDataBound="ASPxGridView1_RowDataBound" OnSorting="ASPxGridView1_Sorting" OnRowUpdating="ASPxGridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<%-- 0 This is e.Row.Cells[0].Text --%>
<asp:BoundField DataField="SALESID" HeaderText="SALES ORDER" SortExpression="SALESID" HeaderStyle-CssClass="padLeft" HeaderStyle-Width="125" ItemStyle-Width="115">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 1 --%>
<asp:BoundField DataField="ITEMID" HeaderText="PART NUMBER" HeaderStyle-HorizontalAlign="Center" SortExpression="ITEMID" ItemStyle-Width="150" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 2 --%>
<asp:BoundField DataField="NAME" HeaderText="PART NAME" HeaderStyle-HorizontalAlign="Center" SortExpression="NAME" ItemStyle-Width="320">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 3 --%>
<asp:BoundField DataField="CUSTACCOUNT" HeaderText="ACC" HeaderStyle-HorizontalAlign="Center" SortExpression="CUSTACCOUNT">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 4 --%>
<asp:BoundField DataField="SALESNAME" HeaderText="CUSTOMER" HeaderStyle-HorizontalAlign="Center" SortExpression="SALESNAME" ItemStyle-Width="350" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 5 --%>
<asp:BoundField DataField="SHIPPINGDATECONFIRMED" HeaderText="SHIPPING DATE" HeaderStyle-HorizontalAlign="Center" SortExpression="SHIPPINGDATECONFIRMED" ItemStyle-Width="100" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 6 --%>
<asp:BoundField DataField="QTYORDERED" HeaderText="QTY ORDERED" HeaderStyle-HorizontalAlign="Center" SortExpression="QTYORDERED" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 7 --%>
<asp:BoundField DataField="REMAINSALESPHYSICAL" HeaderText="QUANTITY REMAINED" HeaderStyle-HorizontalAlign="Center" SortExpression="REMAINSALESPHYSICAL" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 8 --%>
<asp:BoundField DataField="AVAILPHYSICAL" HeaderText="AVAIL PHYSICAL" HeaderStyle-HorizontalAlign="Center" SortExpression="AVAILPHYSICAL" ItemStyle-Width="80" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 9 --%>
<asp:HyperLinkField DataTextField="WMSLOCATIONID" HeaderText="LOCATION" HeaderStyle-HorizontalAlign="Center" DataNavigateURLFields="ITEMID" DataNavigateURLFormatString="~\Location.aspx?ITEMID={0}" Target="_blank" SortExpression="WMSLOCATIONID">
<ItemStyle HorizontalAlign="Center" />
</asp:HyperLinkField>
<%-- 10 --%>
<asp:HyperLinkField DataTextField="INPROCESS" HeaderText="IN PROCESS" HeaderStyle-HorizontalAlign="Center" DataNavigateUrlFields="ITEMID" DataNavigateUrlFormatString="~\InProcess.aspx?ITEMID={0}" Target="_blank" SortExpression="INPROCESS">
<ItemStyle HorizontalAlign="Left" />
</asp:HyperLinkField>
<%-- 11 --%>
<asp:BoundField DataField="PRD" HeaderText="PRD" HeaderStyle-HorizontalAlign="Center" SortExpression="PRD" ItemStyle-Width="135">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 12 --%>
<asp:TemplateField HeaderText="SHIPTODAY" ItemStyle-Width="90">
<ItemTemplate>
<asp:TextBox ID="SHIPTODAY" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<%-- 13 --%>
<asp:TemplateField HeaderText="NOTES" >
<ItemTemplate>
<asp:TextBox ID="NOTES" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" HeaderText="" CommandName="Update" Text="Update" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#284775" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="False" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Here is my gridview _RowUpdating method:
protected void ASPxGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Debug.WriteLine("row updating");
int index = Convert.ToInt32(e.RowIndex);
Debug.WriteLine("Index: " + index);
GridViewRow selectedRow = ASPxGridView1.Rows[index];
TableCell itemIDSelected = selectedRow.Cells[1];
TableCell rowPRD = selectedRow.Cells[11];
TableCell rowShipToday = selectedRow.Cells[12];
TableCell rowNotes = selectedRow.Cells[13];
string itemIdSelectedText = itemIDSelected.Text;
string rowPRDSelected = rowPRD.Text;
string rowShipTodaySelected = rowShipToday.Text;
string test = ((TextBox)selectedRow.FindControl("NOTES")).Text;
string test1 = ((TextBox)ASPxGridView1.Rows[index].FindControl("NOTES")).Text;
TextBox test2 = ASPxGridView1.Rows[index].FindControl("NOTES") as TextBox;
string test2text = test2.Text;
//string rowNotesSelected = rowNotes.Text;
//Debug.WriteLine(itemIdSelectedText);
//Debug.WriteLine(itemIdSelectedText);
//Debug.WriteLine(rowPRDSelected);
//Debug.WriteLine(rowShipTodaySelected);
Debug.WriteLine(test1);
Debug.WriteLine(test);
Debug.WriteLine(test2text);
//Debug.WriteLine(selectedRow.Cells[0].Text);
//Debug.WriteLine(selectedRow.Cells[1].Text);
//Debug.WriteLine(selectedRow.Cells[2].Text);
//Debug.WriteLine(selectedRow.Cells[3].Text);
//Debug.WriteLine(selectedRow.Cells[4].Text);
//Debug.WriteLine(selectedRow.Cells[5].Text);
//Debug.WriteLine(selectedRow.Cells[6].Text);
//Debug.WriteLine(selectedRow.Cells[7].Text);
//Debug.WriteLine(selectedRow.Cells[8].Text);
//Debug.WriteLine(selectedRow.Cells[9].Text);
//Debug.WriteLine(selectedRow.Cells[10].Text);
Debug.WriteLine(selectedRow.Cells[11].Text);
//Debug.WriteLine(selectedRow.Cells[12].Text);
//Debug.WriteLine(selectedRow.Cells[13].Text);
Debug.WriteLine("-----------");
//Debug.WriteLine(rowShipTodaySelected);
//Debug.WriteLine(rowNotesSelected);
}
When I run this and I type something in, the debug writeline's show.
I know i'm on the correct line because the index is correct per the line I am clicking the Update button on.
Sample debug output from above looks like this:
row updating
Index: 5
D31948P01
PRD-00030521
PRD-00030521
I cannot seem to get the value of the NOTES TextBox field.
As you can see with my testing I've tried using a FindControl and I've tried having it as a TextBox and also trying to get it directly to a string.
I've set breakpoints and each time I do it the test variable is null.
I'm missing something and I do not know what.
To summarize:
On non-text fields I can pull the data from the field.
On TextBox fields I cannot even using the FindControl method.
What I want to do:
Get the TextBox field value in the GridView so I can update the database.
StackOverflow links I've tried before asking this question:
c# Get value (text) of a bound textbox in a gridview
How to get value of TextBox of GridView in C#?
Manipulate textbox on gridview C#
Get TextBox value from GridView cell
ASP.net C# Gridview ButtonField onclick event
Thanks in advance. I've spent over 6 hours of attempting this without results. I don't understand it well enough yet. If there are any tutorials or videos explaining GridView's please let me know what they are so I can understand better.
The reason why RowUpdating method cannot find the text inside the textbox is most likely because of the postback. Clicking update button causes a post back to server. Before ASPxGridView1_RowUpdating is called, there is an a call to re-build the gridview ie.: ASPxGridView1.DataBind(). To avoid this, wrap the databind in an if (!Page.IsPostBack)
Below is a basic working example
<asp:GridView ID="ASPxGridView1"
runat="server"
AutoGenerateColumns="False"
OnRowUpdating="ASPxGridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="NOTES" >
<ItemTemplate>
<asp:TextBox ID="NOTES" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" HeaderText="" CommandName="Update" Text="Update" />
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// This code will not be reached if you click update button.
var data = new string[] { "a", "b", "c" };
ASPxGridView1.DataSource = data;
ASPxGridView1.DataBind();
}
}
protected void ASPxGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
GridViewRow selectedRow = ASPxGridView1.Rows[index];
TextBox txtNotes = (TextBox)selectedRow.FindControl("NOTES");
Debug.WriteLine(txtNotes.Text);
}
I´m trying to play around ItemStyle- Width and but this only works if i use Attribites.Add("Style"...) in my page load.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
...
}
gwActivity.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
}
However this is not a problem. My issue is that the column headers are affected in the ItemStyle-Width:... and i do NOT want that to be the case. Can i only play around with the rows and not the headers?
<%--************************ Gridview section ************************--%>
<asp:GridView ID="gwActivity" runat="server" AutoGenerateColumns="False" OnRowCommand="gwActivity_RowCommand" CssClass="gwActivity" >
<Columns>
<asp:BoundField DataField="ActivityID" HeaderText="ActivitID"></asp:BoundField>
</Columns>
<HeaderStyle BackColor="#E6E6E6" Font-Bold="false" Font-Names="Arial" ForeColor="#000000" />
<%-- <AlternatingRowStyle BackColor="#E6E6E6" /> --%>
</asp:GridView>
try this..
<asp:GridView ID="gwActivity" runat="server"
AutoGenerateColumns="False" OnRowCommand="gwActivity_RowCommand"
CssClass="gwActivity" >
<HeaderStyle BackColor="#E6E6E6" Font-Bold="false" ForeColor="#000000" Font-Names="Arial />
<Columns>
<asp:BoundField DataField="ActivityID" HeaderText="ActivitID"></asp:BoundField>
<asp:BoundField DataField="ActivityID" HeaderText="ActivitID" HeaderStyle-Font-Bold="false"
HeaderStyle-Font-Size="12px" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="25">
<HeaderStyle Font-Bold="False" Font-Size="12px" />
<ItemStyle Font-Size="12px" HorizontalAlign="Left" Width="200px" /> //set width you want..
</asp:BoundField>
</Columns>
</asp:GridView>
I have a gridview with paging function.
the code below is my aspx code for gridview.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" Width="768px" CellPadding="4" ForeColor="#333333" OnRowDeleting="OnRowDeleting" AllowPaging="True" PageSize="20" OnPageIndexChanging="gridView_PageIndexChanging" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="SAP No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click"
Text='<%#Eval("SAPNO") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PARTNO" HeaderText="Part No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
</asp:BoundField>
<asp:BoundField DataField="PARTDESC" HeaderText="Description" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:BoundField DataField="MINQTY" HeaderText="Min Qty" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">
</asp:BoundField>
<asp:BoundField DataField="QOH" HeaderText="QOH" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">
</asp:BoundField>
<asp:BoundField DataField="CATEGORY" HeaderText="Category" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:BoundField DataField="EQUIPMENT" HeaderText="Equipment" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:CommandField ShowDeleteButton="true" ButtonType="Image" DeleteImageUrl="Image/delete.JPG" ControlStyle-Height="20px" ControlStyle-Width="50px" HeaderText="Delete">
<HeaderStyle Width="10%" HorizontalAlign="Left" /></asp:CommandField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="<--" PreviousPageText="<" NextPageText=">" LastPageText="-->" />
<PagerStyle HorizontalAlign="Center" BackColor="White" />
</asp:GridView>
At the back end code, i tried to retrieve the first column and specific rows but it throw me empty. the code below is how i retrieve.
int index = Convert.ToInt32(e.RowIndex);
string sapNo1 = GridView1.Rows[index].Cells[0].Text;
string partNo = GridView1.Rows[index].Cells[1].Text;
string partDesc = GridView1.Rows[index].Cells[3].Text;
I try to show all the value for sapNo1, it throw me blank but for partNo it will show the data in the gridview.
anyone has any idea about this?
I am really appreciate your help and comment!
As your first column is a linkbutton so retrieve that as it is then find the value.
string sapNo1 = GridView1.Rows[index].Cells[0].Text; // wrong code to retrieve data from linkbutton in a gridview
Modify this as :
LinkButton linkbtn =(LinkButton)GridView1.Rows[index].FindControl("LinkButton1");
string sapNo1 = linkbtn.Text;
or
string sapNo1 =(GridView1.Rows[index].FindControl("LinkButton1") as LinkButton).Text;
You can't get the controls properties present inside TemplateField like that, you need to find the control like this:-
LinkButton LinkButton1 = (LinkButton)GridView1.Rows[index].Cells[0]
.FindControl("LinkButton1");
After this you can simply fetch the properties of this control:-
string sapNo1 = LinkButton1.Text;
you can get the link button description by the below code
protected void Link_Button_Command(object sender, CommandEventArgs e)
{
try
{
LinkButton button = (LinkButton)sender;
string name = button.ToString();
}
catch (Exception ex)
{
lbl_message.Text = ex.Message;
}
}
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.
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