GridView TemplateField bound to SQL Statement? - asp.net

I have a Gridview with template fields conatining drop down lists. I need to populate the dropdownlists with a sql statement. My gridview code is as such:
<asp:TemplateField HeaderText="Ledger">
<EditItemTemplate>
<asp:DropDownList ID="ddlItemTempLedger" runat="server" Width="61px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="75px" />
</asp:TemplateField>
And the sql statement I need to populate it with is this:
SELECT V_VendorNo + '|' + V-VendorName FROM VendorTbl
Can anyone assist me with this?

You will not to get the dropdownlist in RowDataBound event of grid and assign datasource to it and bind it.
protected void GrdViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlItemTempLedger = e.Row.FindControl("ddlItemTempLedger ") as DropDownList ;
ddlItemTempLedger.DataSource = dt; //DataTable from database
ddlItemTempLedger.DataTextField = "FieldForTextInDataTabledt";
ddlItemTempLedger.DataValueField = "FieldForValueInDataTabledt";
ddlItemTempLedger.DataBind();
}
}

Related

Get index from selected Checkbox in DataGrid

I am using DataGrid in Asp.net
<asp:DataGrid>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkDetail" runat="server" OnCheckedChanged="chkDetail_CheckedChanged" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
As you can see, there is a Checkbox inside DataGrid.
Everytime I check in checkbox, I would like to fire 2 events
1. Other checkboxes will be unselected (I can do it with JS - so it's ok)
2. Context will store DataGrid Name field like this
protected void chkDetail_CheckedChanged(object sender, EventArgs e)
{
Context["Name"] = ??;
}
Because DataGrid doesn't have "Rows" like GridView, I have no idea how to get the Name from the same row.
Thanks in advance.
DataGridViewRow dgvrows = TrGrid.SelectedRows;
string c = dgvrows.Cells("Late_Time").value.toString();
OR
DataGridViewRow dgvrow = this.dataGridView1.Rows[index];
DataRowView drvrow = (DataRowView)dgvrow.DataBoundItem;
You can try this code
protected void chkDetail_CheckedChanged(object sender, EventArgs e)
{
Context["Name"] = (((sender as CheckBox).NamingContainer as DataGridItem).FindControl("label_id") as Label).Text;
}
Your sender will be a CheckBox as the event is triggered by it.
Every control will have a property called NamingContainer which gives the nearest server control that inherits INamingContainer interface. In your case it is DataGridItem. And you can use FindControl method to find child control and cast it to appropriate control type to access its properties.

How to Populate Dropdown inside the Gridview?

I have a dropdown inside the gridview template field.
<asp:templatefield headertext="Bill Period">
<itemtemplate>
<asp:dropdownlist runat="server" id="cboBillPeriod"></asp:dropdownlist>
</itemtemplate>
</asp:templatefield>
I want to populate the dropdown
i could i do it? can any please help me.
You could bind the dropdown in OnRowDataBound event of GridView as follow:
GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField HeaderText="Name" DataField="ContactName" />
<asp:TemplateField HeaderText = "Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' Visible = "false" />
<asp:DropDownList ID="ddlCountries" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
With the help of FindControl method you will be able to get the dropdown control and then you could play with that control.
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
//Add Default Item in the DropDownList
ddlCountries.Items.Insert(0, new ListItem("Please select"));
// Select the Country of Customer in DropDownList
string country = (e.Row.FindControl("lblCountry") as Label).Text;
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
You can use this for your dropdown in your gridview.
<asp:TemplateField HeaderText="Item Condition" HeaderStyle-Width="80px" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="40px>
<ItemTemplate>
<asp:DropDownList ID="ddlConditions" runat="server" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Under your grid "RowDataBound" event, you will bind your dropdown in code behind using dropdown id.
DropDownList ddlConditions2 = (e.Row.FindControl("ddlConditions") as DropDownList);
DataTable dt = _reader.GetDataTableByCommandFromStoredProc("getYourDropdownData");
ddlConditions2.DataSource = dt;
ddlConditions2.DataTextField = "ConditionName";
ddlConditions2.DataValueField = "Id";
ddlConditions2.DataBind();
ddlConditions2.Items.Insert(0, new ListItem("--Select--", "0"));
You have to use RowDataBound event of grid view. For more Check that Link.
In Rowdatabound event of Gridview, try to bind your drop-down.

Bind Data to DropDownList in GridView asp.net

I have web form with a GridView and a few controls in the GridView. I have a DropDownList in the EdtItemTemplate of the gridview.
I am needing to bind this DropDownList with some method in my CodeBehind File that returns a Array of type LisItems.
The problem I am facing is this. Since the Control is sitting in the EditItemTemplate, using the FindControl("MyControlID") does not seem to work in any of the GridView events, it returns null, in other words it cannot seem to find the control, unless I use the OnRowUpdating event, but I cannot use this event as the Control needs to be Data binded before that.
Is there anyway I can use the <%# Bind("MyMethodName") %> to bind the control?
Try this
Create a class of your data in App_Code, Like this
public static class Fruits
{
public static List<string> GetFruits()
{
return new string[] { "Apple", "Mango", "Banana", "Grapes" }.ToList();
}
}
Add a grid to your page, Which I guess you all ready have
<asp:GridView runat="server" ID="grid" AutoGenerateColumns="false" OnRowEditing="grid_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Selected Fruit">
<ItemTemplate>
<asp:Label runat="server" ID="Fruit" Text='<%# Eval("Fruits") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="fruits" DataSourceID="fruitsDS" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button runat="server" ID="btnEdit" Text="Edit" CommandName="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And add an Object Datasource to bind your DropDowns of edit template
<asp:ObjectDataSource ID="fruitsDS" runat="server" SelectMethod="GetFruits" TypeName="Fruits" />
Hope this could help.
Try this
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList dl = (DropDownList)e.Row.FindControl("myList");
dl.DataSource = new string[] { "A", "B" };
dl.DataBind();
}
}
gridview rowdatabound will use to bind the data to dropdown in gridview.
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlcity");
ddl.DataSource = s;

show data in outside textbox from selected row in gridview

I am having a grid view created by using template fields. I inserted a link button using template fields in the grid view.
There are 4 textboxes outside the GridView.. i want to select the row on link button's click and put the selected row's data in text boxes. I am using a row command even for this but its not working ... the syntax i am using is .:
<asp:GridView ID="gview" AutoGenerateColumns="False" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" onrowcommand="gview_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Book Name">
<ItemTemplate>
<%#Eval("book_name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Author">
<ItemTemplate>
<%#Eval("book_author") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Publisher">
<ItemTemplate>
<%#Eval("book_Publisher") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Price">
<ItemTemplate>
<%#Eval("book_Price") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Update">
<ItemTemplate>
<asp:LinkButton ID="lnkDet" CommandName="cmdBind" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" runat="server" CausesValidation="false">View Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
and Code behind file :
protected void gview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdBind")
{
LinkButton lb = (LinkButton)e.CommandSource;
int index = Convert.ToInt32(lb.CommandArgument);
//Bind values in the text box of the pop up control
txt_name.Text = gview.Rows[index].Cells[0].Text;
txt_author.Text = gview.Rows[index].Cells[1].Text;
txt_price.Text = gview.Rows[index].Cells[2].Text;
}
}
Can anyone tell me how to do this.
I think this works:
protected void gview_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName == "cmdBind")
{
int index = Convert.ToInt32(e.CommandArgument);
//Bind values in the text box of the pop up control
txt_name.Text = gview.Rows[index].Cells[0].Text;
txt_author.Text = gview.Rows[index].Cells[1].Text;
txt_price.Text = gview.Rows[index].Cells[2].Text;
}}
Attempting it the way you are is not impossible, but it may be easier to just use the in built select buttons for each row. That way you can just use the SelectedIndexChanged event of the gridview:
protected void gview_SelectedIndexChanged(object sender, EventArgs e)
{
txt_name.Text = gview.SelectedRow.Cells[0].Text;
txt_author.Text = gview.SelectedRow.Cells[1].Text;
txt_price.Text = gview.SelectedRow.Cells[2].Text;
}

How to select particular row in a gridview

I have a Gridview
<asp:GridView ID="GridView1" runat="server" Width="400px" AutoGenerateColumns="false"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="lblStudentName" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblResidentialAddress" runat="server" Text='<%# Eval("ResidentialAddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and i get the value binded to the gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt = objdb.GetData("Getsamples", new object[] { });
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
Response.Write(GridView1.SelectedRow.Cells[0].Text);
// string selectedText = ((Label)GridView1.SelectedRow.FindControl("lblStudentName")).Text;
// Response.Write(selectedText);
}
i cannot able to retrive the row where the checkbox is checked...
How to select particular row in a gridview, and based upon the selection i need to take out the 'Name' and pass this as a parameter to get ,another gridview related to the row which i selected.???
any help...
Try using another event - OnSelectedIndexChanging (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanging.aspx)
It has GridViewSelectEventArgs (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewselecteventargs_members.aspx) passed to the event handler which has NewSelectedIndex property.
Your event handler will look like:
void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
}

Resources