Getting selectedrow in RowCommand - c#

i have a gridview as:
<asp:GridView ID="gdvOpinions" runat="server" Width="100%" CellPadding="4"
ForeColor="#333333" GridLines="None" Height="100px" Visible="False"
AutoGenerateColumns="False" onrowcommand="gdvOpinions_RowCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Confirm" Text="تائید" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="حذف" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="مقاله" HeaderText="مقاله" >
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="نظر کاربر" HeaderText="نظر کاربر" >
<ControlStyle Width="250px" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
As you see, i have two buttonfield for every row, but when i click one of these buttons and want to get their row in
gdvOpinions_RowCommand
the
gdvOpinions.SelectedRow
returns null;
how should i find which row is selected?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
OnRowCommand = "OnRowCommand">
<Columns>
<asp:ButtonField CommandName = "ButtonField" DataTextField = "CustomerID"
ButtonType = "Button"/>
</Columns>
</asp:GridView>
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
}

From what I understand out of your question, if you are trying to get the row for which the button was clicked, then in that case this might be the right thing to do :
GridViewRow gRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
Now here, this is for a control such as a Button or ImageButton. You might have to improvise on this for ButtonField.
You can also change the ButtonField to a simple TemplateField with a Button.
Hope this helps.

Related

How to track a specific Button from gridview

I have a gridview with template button in it each row
each button fires an Onclick event.
i need to track the row of the pressed button.
if i want , lets say colorize the background , how should i do it?
<asp:GridView ID="OrdersGridView" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="DeleteOrder" runat="server" BackColor="Red" Font-Bold="True" ForeColor="White" Text="מחק" OnClick="DeleteOrder_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="OrderHost" HeaderText="OrderName" />
<asp:BoundField DataField="OrderId" Visible="False" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
The event is empty.
protected void DeleteOrder_Click(object sender, EventArgs e)
{
}
Based from your comment, you just want to get the index of your GridView.
protected void DeleteOrder_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
//Get the Row Index by calling row.RowIndex;
}
Hope it helps!

How to Bind Selected Rows From Grid View to Another Grid View

I would to add the selected rows from gridview into another gridview. Could someone help me on this? When I try to bind gridview to another gridview, another gridview doesn't appear.
The aspx file I posted below is about how I created the gridview. The gridview1 will get its gridview contents from the database table. While the cs file is about how I am going to bind selected rows from gridview1 into gridview2. The problem now is when I try to bind the rows over, the gridview2 didn't appear, only gridview1 appeared.
This is my codes:
ASPX file:
<h3 class="h3">Grid View1</h3>
<div style="width: 100%; height: 400px; overflow: auto">
<asp:GridView ID="GridView1"
runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
Width="100%"
CellPadding="6"
ForeColor="#333333"
GridLines="Horizontal"
BorderColor="Black"
BorderStyle="Solid"
BorderWidth="2px"
EmptyDataText="Record Not Found"
OnRowDataBound="GridView1_OnRowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="DATE"
HeaderText="DATE"></asp:BoundField>
<asp:BoundField DataField="CODE"
HeaderText="CODE"></asp:BoundField>
<asp:BoundField DataField="PROFILE_NAME"
HeaderText="PROFILE_NAME"></asp:BoundField>
<asp:BoundField DataField="DESCRIPTION"
HeaderText="DESCRIPTION"></asp:BoundField>
<asp:BoundField DataField="STATUS"
HeaderText="STATUS"></asp:BoundField>
<asp:BoundField DataField="USER"
HeaderText="USER"></asp:BoundField>
<asp:BoundField DataField="SUB_USER"
HeaderText="SUB_USER"></asp:BoundField>
<asp:BoundField DataField="SCORE"
HeaderText="SCORE"></asp:BoundField>
<asp:BoundField DataField="ROLE"
HeaderText="ROLE"></asp:BoundField>
<asp:BoundField DataField="QUANTITY"
HeaderText="QUANTITY"></asp:BoundField>
<asp:BoundField DataField="ITEM"
HeaderText="ITEM"></asp:BoundField>
<asp:BoundField DataField="PRICE"
HeaderText="PRICE"></asp:BoundField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1"
Font-Bold="False"
ForeColor="Black" />
<HeaderStyle BackColor="#507CD1"
Font-Bold="False"
ForeColor="Black"
BorderStyle="Solid"
BorderWidth="2px" />
<PagerStyle BackColor="#2461BF"
ForeColor="White"
HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1"
Font-Bold="False"
ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<h3 class="h3">Grid View2</h3>
<div style="width: 100%; height: 400px; overflow: auto">
<asp:GridView ID="GridView2"
runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
Width="100%"
CellPadding="6"
ForeColor="#333333"
GridLines="Horizontal"
BorderColor="Black"
BorderStyle="Solid"
BorderWidth="2px"
EmptyDataText="Record Not Found"
OnRowDataBound="GridView2_OnRowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="DATE"
HeaderText="DATE"></asp:BoundField>
<asp:BoundField DataField="CODE"
HeaderText="CODE"></asp:BoundField>
<asp:BoundField DataField="PROFILE_NAME"
HeaderText="PROFILE_NAME"></asp:BoundField>
<asp:BoundField DataField="DESCRIPTION"
HeaderText="DESCRIPTION"></asp:BoundField>
<asp:BoundField DataField="STATUS"
HeaderText="STATUS"></asp:BoundField>
<asp:BoundField DataField="USER"
HeaderText="USER"></asp:BoundField>
<asp:BoundField DataField="SUB_USER"
HeaderText="SUB_USER"></asp:BoundField>
<asp:BoundField DataField="SCORE"
HeaderText="SCORE"></asp:BoundField>
<asp:BoundField DataField="ROLE"
HeaderText="ROLE"></asp:BoundField>
<asp:BoundField DataField="QUANTITY"
HeaderText="QUANTITY"></asp:BoundField>
<asp:BoundField DataField="ITEM"
HeaderText="ITEM"></asp:BoundField>
<asp:BoundField DataField="PRICE"
HeaderText="PRICE"></asp:BoundField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1"
Font-Bold="False"
ForeColor="Black" />
<HeaderStyle BackColor="#507CD1"
Font-Bold="False"
ForeColor="Black"
BorderStyle="Solid"
BorderWidth="2px" />
<PagerStyle BackColor="#2461BF"
ForeColor="White"
HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1"
Font-Bold="False"
ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
CS file:
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, “USER”)) == “ALAN TAN”)
{
DataTable dt = new DataTable();
dt.Columns.Add(“DATE”);
dt.Columns.Add(“CODE”);
dt.Columns.Add(“PROFILE_NAME”);
dt.Columns.Add(“DESCRIPTION”);
dt.Columns.Add(“STATUS”);
dt.Columns.Add(“USER”);
dt.Columns.Add(“SUB_USER”);
dt.Columns.Add(“SCORE”);
dt.Columns.Add(“ROLE”);
dt.Columns.Add(“QUANTITY”);
dt.Columns.Add(“ITEM”);
dt.Columns.Add(“PRICE”);
DataRow dataRow;
dataRow = dt.NewRow();
int i2 = 1;
for(int i=0; i<dataRow.Table.Columns.Count; i++)
{
dataRow[i] = GridView1.SelectedRow.Cells[i2].Text;
i2++;
}
dt.Rows.Add(dataRow);
GridView2.DataSource = dt;
GridView2.DataBind();
}
}
}
Any help is greatly appreciated. Thanks!
I Really didn't understand messy code done by you (Too many things are missing in your question). Refer below links that will explain you will example.
http://www.aspsnippets.com/Articles/Transfer-Selected-Rows-from-one-GridView-to-Another-in-Asp.net.aspx
http://www.ittutorials.in/source/aspDotnet/scf13/move-gridview-rows-from-one-gridview-to-another.aspx
Updates:
dt.Rows.Add() must be inside of for loop so everytime it will get add new row.
try to update following code in cs file
for(int i=0; i<dataRow.Table.Columns.Count; i++)
{
dt.Rows.Add(GridView1.SelectedRow.Cells[i2].Text);
i2++;
}
GridView2.DataSource = dt;
GridView2.DataBind();

gridview disappears after changing the page

I have a gridview that should split into pages but the issue is that when I change the page the whole gridview is disappearing I tried everything I found in the internet but with no solution here is my code
<asp:GridView ID="ExistContents" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
AllowPaging="true" PageSize="5" OnPageIndexChanging="ExistContents_PageIndexChanging" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="ContentID" HeaderText="id" />
<asp:ImageField DataImageUrlField="TmpFilename" HeaderText="Image">
<ControlStyle Height="64px" Width="96px" />
</asp:ImageField>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Type" HeaderText="Type" />
<asp:BoundField DataField="ContentID" HeaderText="id" Visible="false" ShowHeader="false" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:CheckBox runat="server" ID="ChBox1" OnCheckedChanged="ExistContents_CheckedChanged" AutoPostBack="True"/>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="header"/>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
and here is the event handler
protected void ExistContents_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ExistContents.PageIndex = e.NewPageIndex;
List<CONTENT> panier;
panier = (List<CONTENT>)Session["PANIER"];
ExistContents.DataSource = panier;
ExistContents.DataBind();
}
You should handle the PageIndexChanged event instead.
The pageIndexChanging is made to give you an option to cancel the paging before it's executed.
here is an answer for this probleme, acctually my datasource was not the session but a linq query so i changed it and it works perfectly
and here is my new code
protected void ExistContents_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow gr = (GridViewRow)chk.Parent.Parent.Parent.Parent;
int id= Convert.ToInt32(gr.Cells[0].Text);
if (chk.Checked)
AddToCaddy(id, "DELETE");
else
DeleteFromCaddy(id);
UpdatePanel.DataBind();
UpdatePanel.Update();
}
just for information if you are making a databind to your gridview in the Page_Load inside the if (!IsPostBack) then you will need to redefind the dataSource but if you'r not then you only need to use these functions
UpdatePanel.DataBind();
UpdatePanel.Update();
i hope this helps

dynamically set width of a column in Gridview

I am trying to dynamically set width of gridview columns but when I apply the following code, I see no change in the browser.
.aspx code is:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" CellSpacing="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="File_Name" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_OnRowDataBound">
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="File">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnFileName" runat="server" CommandArgument='<%# Eval("File_Name") %>' CommandName="Download" Text='<%# Eval("File_Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
.cs code is:
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//SET WIDTH OF THE COLUMNS
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Width = 100; //DATE
e.Row.Cells[1].Width = 100; //CUSTOMER
e.Row.Cells[2].Width = 100; //DESTINATION
}
}
Can anyone notice where I might be going wrong?
Please check .. I think you may need to do the following way
e.Row.Cells[0].Width = Unit.Pixel(500);
e.Row.Cells[1].Width = Unit.Pixel(500);

How do I hide my primary key from GridView but still reference it

I have a GridView that is built from a Linq query as follows
var GridViewLoad = from d in QVuser.QlikViewDashboards.AsEnumerable()
join p in tempPermissions.AsEnumerable() on d.DashboardId equals Convert.ToInt32(p["DashboardId"])
where Convert.ToInt32(p["UserId"]) == GridViewUser
select new
{
DashboardId = d.DashboardId,
PermissionId = Convert.ToInt32(p["PermissionId"]),
DashboardName = d.DashboardName,
Operational_Unit = p["Operational_Unit"].ToString(),
Cost_Centre = p["Cost_Centre"].ToString(),
Project = p["Project"].ToString(),
Fund = p["Fund"].ToString()
};
GridView1.DataSource = GridViewLoad;
GridView1.DataKeyNames = new string[] {"PermissionId"};
GridView1.DataBind();
Then the GridView is defined as:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"
GridLines="Vertical" ShowHeaderWhenEmpty="True" OnRowDeleting="GridView1_RowDeleting"
style="text-align: center" BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px" Width="550px"
>
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="false"
CommandArgument='<%# Eval("PermissionId") %>' CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PermissionId" HeaderText="ID"/>
<asp:BoundField DataField="DashboardName" HeaderText="Dashboard" ReadOnly="True" SortExpression="DashboardName" />
<asp:BoundField DataField="Operational_Unit" HeaderText="Operational_Unit" ReadOnly="True" SortExpression="Operational_Unit" />
<asp:BoundField DataField="Cost_Centre" HeaderText="Cost_Centre" ReadOnly="True" SortExpression="Cost_Centre" />
<asp:BoundField DataField="Fund" HeaderText="Fund" ReadOnly="True" SortExpression="Fund" />
<asp:BoundField DataField="Project" HeaderText="Project" ReadOnly="True" SortExpression="Project" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" BorderStyle="Solid" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" BorderStyle="Solid"
BorderWidth="1px" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
What I want to do is hide the PermissionId field from sight, but it obviously still needs to be there for the Delete button to work..
Can someone help me out with this?
I've tried setting Visible="false", which hides it, but then my delete button stops working..
Cheers for your help..
Since you're setting the DataKeyNames, you should be able to retrieve the permission ID from the index of the row being deleted, like so:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int permissionId = (int)GridView1.DataKeys[e.RowIndex].Value;
DoDelete(permissionId);
}
This should work whether the column is visible or not.
Try
<asp:BoundField DataField="PermissionId" HeaderText="ID" Visible="False"/>

Categories

Resources