I have a gridview in ASP.NET with a item template which holds a drop down list. I have an event for the drop down list (selected index changed) in which I need to figure out which row, in the gridview, this control resides in. Anyone know how to accomplish this? (I add more rows later, so this is why I need the row I am in.)
<asp:gridview ID="grdTest" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Query Line" />
<asp:TemplateField HeaderText="Table Name">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// TODO: Grab the row of the control here!
int row = 0;
DropDownList ddl = (DropDownList)sender; // How?
}
you can do it like this,
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
GridViewRow row = (GridViewRow) ddl.NamingContainer;
int rowIndex = row.RowIndex;
}
Below code will help you
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim gvrow As GridViewRow = CType(sender, DropDownList).NamingContainer
Dim index As Integer = CType(gvrow, GridViewRow).RowIndex
End Sub
Related
I need to get a row id from database with the button click.
This is my c# code:
protected void ddlBC_SelectedIndexChanged(object sender, EventArgs e)
{
LogicTableAdapters.getLvLKarTableAdapter getKar = new LogicTableAdapters.getLvLKarTableAdapter();
DataTable dtKar = getKar.getLvLKar(ddlBC.SelectedValue);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2]
{
new DataColumn("CharName", typeof(string)),
new DataColumn("LevelID", typeof(int))
});
foreach (DataRow dr in dtKar.Rows)
{
dt.Rows.Add(dr["CharName"].ToString(), dr["LevelID"].ToString());
}
gvKar.DataSource = dt;
gvKar.DataBind();
}
protected void btnButton_Click(object sender, EventArgs e)
{
}
This is dynamically GridView that gets populated by the procedure when item in ddl is selected:
<asp:GridView ID="gvKar" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="LevelID" OnRowDataBound="gvKarakteristike_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Kar">
<ItemTemplate>
<asp:Label ID="Kar" runat="server" Width="150px" Height="30px" Font-Names="Georgia" MyCustomAttr="foo" margin-Left="100px" Text='<%# Bind("CharName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnButton" runat="server" Text="Show" autopostback="True"/>
When the button is clicked in form of alert row id from every row must be shown from database.
Can anyone help me please ?
I use code like this to assign a click handler to the row:
Protected Sub Grid_RowDataBound(sender As Object, e As GridViewRowEventArgs)
'apply click attribute to select a row
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(sender, "Select$" & e.Row.RowIndex)
End If
End Sub
Then this to get the relevant data from the datakeys collection
Protected Sub Grid_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Select" Then
Dim LevelID As String = GridResults.DataKeys(e.CommandArgument).Item("LevelID")
' Do something with ID here
End If
End Sub
I have a gridview, inside the gridview is a textbox on templatefield, how do i get the row index of the clicked textbox when i change its value?
<asp:GridView ID="productView" runat="server" BorderWidth="3px" CellPadding="4" CellSpacing="2" AutoGenerateColumns="False" Width="1000px" OnSelectedIndexChanged="productView_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Sacks">
<ItemTemplate>
<asp:TextBox ID="txtSacks" runat="server" CssClass="form-control" Text ="0" Width="100px" OnTextChanged="txtSacks_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs:
protected void txtSacks_TextChanged(object sender, EventArgs e)
{
//Rowindex of the texbox
}
Get it using RowIndex
GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
int index = gvRow.RowIndex;
You can get the GridViewRow via the NamingContainer property of the TextBox:
protected void txtSacks_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox) sender;
GridViewRow row = (GridViewRow) txt.NamingContainer;
int rowIndex = row.RowIndex;
}
This is better than using txt.Parent.Parent because it still works even with nested controls (f.e. if you want to add the TextBox to a Table or Panel).
I have a gridview that displays data coming from an SQL data source (stored procedure) and I want to add a checkboxex column to it, here is my code:
TemplateField field = new TemplateField();
field.HeaderText = "Exporter ?";
gv.Columns.Add(field);
CheckBox cb = new CheckBox();
cb.Visible = true;
The problem is that I don't know how to add a checkbox to the TemplateField that I added to my gridview columns.
1) Add below code to GridView columns.
<asp:TemplateField HeaderText="CheckBoxColumn" Visible="False">
<ItemTemplate>
<asp:CheckBox ID="checkBox" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
2) Make checkbox column visible dynamically by adding OnRowDataBound event or just looping through GridView.Rows
int indexOfCBColumn = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[indexOfCBColumn].Visible = true;
}
foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
row.Cells[indexOfCBColumn].Visible = true;
}
}
Thanks!!
You can add the code below into the GridView Columns.
<asp:templatefield HeaderText="Check Box">
<itemtemplate>
<asp:checkbox ID="cb" runat="server"></asp:checkbox>
</itemtemplate>
</asp:templatefield>
I'm new to ASP.NET and I am using GridView to display data on a web page. I also added a button in Gridview called process orders.
<asp:GridView ID="GridView1" runat="server" autogenerateselectbutton="True" GridLines="None" AllowPaging ="true"
OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Process" runat="server"
CommandName="processorders"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Process orders" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I used the row command method to get the selected index of the row.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "processorders")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
}
Also I've implemented selectedIndexChanged method for the default select button:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
txtbox.Text = "You selected " + row.Cells[3].Text + ".";
}
If I try to use the GridViewRow in GridView1_RowCommand I get a null pointer exception. The row is always null but it works fine in GridView1_SelectedIndexChanged.
What am I trying to achieve is, get the column value from the selected row using my Process button and then use that value to update a database. I don't want to do it using Select.
And is there any way I can use row.Cells[] without specifying the index. I want to get the value of a column name.
Help? Please ??
I have a grid view with a SelectedIndexChanged event that fires when the user selects a row.
Now in the grid view row, I add a drop down list using TemplateField, and in the grid view RowDataBound event I add code for firing the SelectedIndexChanged event of the drop down list.
So, when user click on drop down list the first event that fires is the SelectedIndexChanged of the grid view, then page goes into post back and the selection of drop down list is lost.
The SelectedIndexChange of drop down list fires only if user is faster on changing selection than page goes into post back.
I need that when user select a row with drop down list grid view's event wait for the drop down list selection and after first event that can fire is drop down list changing and at last grid view's SelectedIndexChanged event.
Is this possible?
some code :
<asp:GridView ID="grEventi" runat="server" BackColor="White"
ShowHeaderWhenEmpty="True" AutoGenerateColumns="False"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" Width="100%" OnRowDataBound="grEventi_RowDataBound"
onselectedindexchanged="grEventi_SelectedIndexChanged" >
<SelectedRowStyle CssClass="selectedRow" />
<Columns>
<asp:BoundField DataField="Elenco Eventi" HeaderText="Evento" />
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-Width="150" HeaderText="Data Inizio">
<ItemTemplate>
<asp:Label ID="lbl_data" runat="server" Text="" Visible="false" >
</asp:Label>
<asp:DropDownList ID="ddl_data" runat="server" Visible="false" OnSelectedIndexChanged="ddl_dat_SelectedIndexChanged" ClientIDMode = "Static" class ="calendar">
</asp:DropDownList>
</asp:TemplateField>
</Columns>
</asp:GridView>
code-behind:
protected void ddl_dat_SelectedIndexChanged(object sender, EventArgs e)
{
//your logic goes here
string test = "";
}
protected void grEventi_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", this.ClientScript.GetPostBackEventReference((Control)sender, "Select$" + e.Row.RowIndex));
DropDownList ddl_dat = (DropDownList)e.Row.FindControl("ddl_data");
ddl_dat.SelectedValue = DataBinder.Eval(e.Row.DataItem, "data inizio").ToString();
ddl_dat.Visible = true;
ddl_dat.DataTextFormatString = "{0: ddd d/MM/yyyy HH:mm}";
ddl_dat.DataTextField = "data inizio";
ddl_dat.DataValueField = "data inizio";
ddl_dat.DataSource = mydata;
// ddl_dat.AutoPostBack = true;
ddl_dat.DataBind();
}
else
{
Label lbl_data = (Label)e.Row.FindControl("lbl_data");
lbl_data.Visible = true;
DateTime date=(DateTime)dr.Row["data inizio"];
lbl_data.Text = date.ToString("ddd d/MM/yyyy HH:mm");
}
}
protected void grEventi_SelectedIndexChanged(object sender, EventArgs e)
{
//my logic code
}