Get the index of a row in a table from code behind - c#

I have a table with two columns and multiple rows, the first column has names and the second one has buttons, how can I get the name next to the button when I click any button.
The names will be taken from a database and the buttons will be generated from code behind depending on the number of names I have in my database.
Let's say that I have this:
<asp:table runat="server" id="table1">
<tr><td>Name1</td><td><asp:Button runat="server" Text="Save"/></td></tr>
<tr><td>Name2</td><td><asp:Button runat="server" Text="Save"/></td></tr>
<tr><td>Name3</td><td><asp:Button runat="server" Text="Save"/></td></tr>
</asp:table>
How can I know which name is next to the button, when I click any of them, I want to get the name so that I can save it in a database.

use for your designer code something similar to this:
<asp:GridView ID="GridView" runat="server" OnRowCommand="gvUsers_RowCommand" AutoGenerateColumns="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="detail_LB" runat="server" Text="Detail" CommandName="Detail_LB" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Notice the OnRowCommand and for the LinkButtons the CommandName and CommandArgument. The argument contains the row number, where you clicked the button.
In your code behind:
protected async void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Detail_LB")
{
//index contains the row number
int index = Convert.ToInt32(e.CommandArgument);
//get text in the cell next to the button
string text = gvUsers.Rows[index].Cells[0].Text;
}
}
If hope this helps you to understand how to work with tables.

Related

How to Find the Inner Gridview in a nested GridView via external button click event?

I have an issue with nested Gridview!
I have two Girdviews with the ID - GridView1(Parent Gridview) and GV2(Child Gridview)
I have a Asp:Button with the ID - btnedit(Event = OnClick())
Qs: how to access GV2 in btnedit_Onclick event?
I have attached the Code for your Reference:
<form id="form1" runat="server">
<asp:Button runat="server" ID="btnedit" Text="Edit" OnClick="btnedit_Click" />
<asp:GridView runat="server" ID="Gridview1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="Lable1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView runat="server" ID="GV2">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="Lable2"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
code Behind:
protected void btnedit_Click(object sender, EventArgs e)
{
}
Ok, so the user wants to operate on the row 1 of main GV and then get row 1 of the child.
Gridviews start at 0, so it not clear, but lets take the both first rows.
next up? I really (but really really really) suggest you use a main listview and then nest a child GV.
If you nest two GV's, you tend to get this:
Say this GV, with a "+" to expand the child grid:
And expanding, you get this:
So, nesting grids is VERY hard, since the "col span" is hard to setup.
However, if I build a main listview (looks the same as a GV), and then next a gv, then I get this:
So above uses a main list view, and thus for expanding child reocrds I do and did use a GV in the child of the listview.
I can post how above works and even the markup if you wish.
However, lets go with the two nested GV's - it will make a mess of a UI but for learning, how this works with the above nested LV + GV, or a GV + GV is the same.
So, some plane jane button - outside of the two grids, get the first row of the main, then the first row of the child grid.
This works:
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow MainGvRow = GridView1.Rows[0];
GridView ChildGV = MainGvRow.FindControl("GridView2") as GridView;
GridViewRow ChildGVRow = ChildGV.Rows[0]
}
This may get you started...
GridView gvGV2 = e.Row.FindControl("GV2") as GridView;
gvGV2.DataSource = "..."; //Your DataSource
gvGV2.DataBind();
// or do something else.

Foreach not getting all the values from gridview because of datatable pagination

So I have a function that loops through a GridView, which is using a JQuery DataTable with pagination. It shows ten rows for every page, so it is necessary to edit all the textboxes you want and click on a button named "Save" that calls the function mentioned before. The issue is that only the page that is displayed on the screen at the moment of clicking on the button "Save" is sending the textbox values and not all gridview texbox instead. For instance, if I edit "gridview page 1", "gridview page 2" and "gridview page 3" fields and click on the button "Save" while "gridview page 3" is displayed then "gridview page 1" and "gridview page 2" textbox values are not saved. If I'm explaining myself wrongly let me know please.
ASPX Inline code
<asp:GridView ID="grvServicio" runat="server" AutoGenerateColumns="false" class="display" >
<Columns>
<asp:BoundField DataField="intIdServicio" HeaderText="ID" />
<asp:BoundField DataField="vchServicio" HeaderText="Servicio" />
<asp:TemplateField HeaderText="Cantidad">
<ItemTemplate>
<asp:TextBox ID="txtCantidad" runat="server" ClientIDMode="Static" Text='<%# Bind("intCantidad")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Precio">
<ItemTemplate>
<asp:TextBox ID="txtPrecio" runat="server" ClientIDMode="Static" Text='<%# Bind("decPrecio")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Estatus">
<ItemTemplate>
<asp:CheckBox ID="chkServEstatus" Checked='<%# Convert.ToBoolean(Eval("intEstatus"))%>' enabled="true" runat="server" CssClass="ChkBoxClass"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
ASPX Behind code (I've removed the code saving process but if necessary let me know)
protected void btnGuardarServicio_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grvServicio.Rows)
{
for (int i = 0; i < grvServicio.Columns.Count; i++)
{
string idEvento = row.Cells[0].Text;
TextBox cant = (TextBox)row.Cells[2].FindControl("txtCantidad");
TextBox prec = (TextBox)row.Cells[3].FindControl("txtPrecio");
prec.Text = prec.Text.Replace(",", ".");
}
}
}
Here is how my GridView looks like using datatable JQuery plug-in
With a paginated grid, data is rendered and loaded (if it is a server -side pagination) only when you change the page. Maybe you can save the data on value changed event so when you change to next page and back again you have data updated.

Data Grid View Hyper Link in ASP.Net C#

Hi I am using DataGridView by mapping URL from database which i have given it in Hyperlinkfield (for eg: there will be 10-20 links will be displayed in the datagridview) if the particular person clicks the particular link then it has to redirect to that particular URL by incrementing the Count column in the Database for that particular URL.
Note : iam using the datagridview in the template design mode.
You can do it in the row command event
create a dynamic click with the url you wish to give
use CommandArgument and do your stuff in Gridview onrowcommand event.
<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnUpdate" runat="server" CommandArgument='<%#Eval("LinkID")%>' CommandName="btnUpdate" Text='<%#Eval("LinkDisplayText")%>'>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="btnUpdate")
{
int index = Convert.ToInt32(e.CommandArgument);
//based on LinkID get the current click count from database.
int icount;
//increment the count
//update the database once again and get the link as well.
//redirect to the link
Response.Redirect("");
}
}

C# SQL Insert records or delete records by GridView

I am using C# Visual Studio 2005 and SQL Server 2005.
I have a GridView table which shows a list of data from my SQL Server database.
I establish my connection via PageLoad method. E.g.:
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "SELECT * FROM [UserTable]";
SqlDataSource1.DataBind();
}
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
However, I am trying to add a function for my Gridview table:
To enable removal of records from the GridView via checkbox of each row of record and a remove button.
The output I want is a checkbox for each row of data of the Gridview, and a 'Remove' button which delete all the records with their checkbox 'checked'.
May I know how can I approach to the outcome that I want?
Thank you
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" OnCheckedChanged="--your events--" runat="server" />
<asp:LinkButton ID="lnkDelete" runat="server" onclick="lnkDelete_Click" Text="Delete"
CommandArgument='<%# Eval("id") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
on lnkDelete_Click event
LinkButton lnk = (LinkButton)sender;
int stid = Convert.ToInt32(lnk.CommandArgument);
now you have id of table which is primary key and you put your delete code here.

Gridview Header customizing issue

If we add a customer header to the gridview, will it add extra row?
Currently I have a gridview with four columns and, when I add a custom header gridview coming with a five rows.
My code looks like this...
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:DropDownList ID="Select" runat="server">
<asp:ListItem>Country</asp:ListItem>
<asp:ListItem>Region</asp:ListItem>
<asp:ListItem>Title</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HiddenField ID="Id" Value='<%#Eval("id")%>' runat="server" />
<asp:Literal ID="ltrImage" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
and three other TemplateFields...
Is there any problem of adding header this way? Any other way to add customer header without having this issue?
My desired output should look like this
Search Result Search By (Dropdownlist)
Data column1 Data column2 Data column3 Data column4
The one I am getting now is
Sort By (Dropdownlist)
Data column1 Data column2 Data column3 Data column4
Could anyone help if have an idea?
Thanks in advance
You just added another column with custom header to your gridview. If you want to customize a header of the first column, just customize the header of your first template field:
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:DropDownList ID="Select" runat="server">
<asp:ListItem>Country</asp:ListItem>
<asp:ListItem>Region</asp:ListItem>
<asp:ListItem>Title</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="Id" Value='<%#Eval("id")%>' runat="server" />
<asp:Literal ID="ltrImage" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
If this dropdownlist is too big or if you want add some additional text in the header, you can always create HeaderTemplate for other TemplateFields of merge some of the column's headers in code behind (example for "merging" two first headers, Id of the gridView is gridView1):
protected void gridView1_PreRender(object sender, EventArgs e)
{
int indexOfColumnToSpan = 0;
int indexOfColumnToRemoveHeader = 1;
gridView1.HeaderRow.Cells[indexOfColumnToSpan].ColumnSpan = 2;
gridView1.HeaderRow.Cells.RemoveAt(indexOfColumnToRemoveHeader);
}
well, you have not actually added a header row, but a header column .. which is basically just another column with more controls ..
why dont you place your DropDownList before your GridView, and then you can write some code behind handling the DropDownList_SelectedIndexChanged event to sort the Data ?
I have implemented this without using the filters within the GridView. Then you can handle the events separately, and populate the GridView accordingly.
What you are actually adding is a column, instead of a row. That's why you are getting that result.
Hope this helps!

Categories

Resources