Can anybody give me some idea about how to create an ASP.NET form like Oracle tabular form.
Like I attached below.
<asp:GridView ID="OracleView" runat="server" AutoGenerateColumns="false" Width="100%" OnRowDataBound="OracleView_RowDataBound">
<Columns>
<asp:BoundField HeaderText="L id" DataField="id"></asp:BoundField>
<asp:TemplateField HeaderText="Concern">
<ItemTemplate>
<asp:DropDownList ID="ConcernList" runat="server" AutoPostBack="true" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Inside OracleView_RowDataBound you add data to your
protected void OracleView_RowDataBound(object sender, GridViewRowEventArgs e)
{
string sql = "select Concern.name from Concern";
/*here your connection and method read list*/
List<string> ls = new List<string>();
ls = return your list data;
//ls.Insert(0, String.Empty);
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl = e.Row.FindControl("ConcernList") as DropDownList;
if (ddl != null)
{
ddl.DataSource = ls;
ddl.SelectedValue = YourSelectedValueFromAnotherTable;
ddl.DataBind();
listDropdownMark = ls;
selectedMeltDropdown = YourSelectedValueFromAnotherTable;
}
}
}
Sorry if I don't help you.
Related
I have a DropDownList that I would like to populate with column values from a DataBase. However, when I try to bind the DropDownList in code behind, the IDE keeps telling me:
"The name 'EqpCatDDL' does not exist in the current context"
I am not sure what is going on since I referred to the control by its ID. The following is the code:
aspx:
<asp:GridView ID="Gridview1" runat="server" ShowFooter="true"
AutoGenerateColumns="false"
>
<Columns>
<asp:BoundField DataField="S/N" HeaderText="S/N" />
<asp:TemplateField HeaderText="Item Name">
<ItemTemplate>
<asp:DropDownList ID="EqpCatDDL" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:DropDownList ID="DescripDDL" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" onclick="ButtonAdd_Click" runat="server" Text="Add New Row" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
c#:
public void Populate1()
{
string connString = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("SELECT EqpCateID, EqpCat FROM EqpCategory", connection);
cmd.Connection.Open();
SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();
EqpCatDDL.DataSource = ddlValues;
EqpCatDDL.DataValueField = "EqpCateID";
EqpCatDDL.DataTextField = "EqpCat";
EqpCatDDL.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
protected void Page_Load(object sender, EventArgs e)
{
Populate1();
}
The IDE can't find the EqpCatDDL control.
I am using the following: Visual Studio 2010, Microsoft SQL Server Management Studio 2008
I am working with a Visual Studio website
Use this code to bound data to dropdown without using RowDataBound.
Create a function that'll bind the Data to dropdown as follow and call it in Page_Load event
Public void fill_gridView_dropDown()
{
// your connection and query to retrieve dropdown data will go here
// this loop will go through all row in GridView
foreach(GridViewRow row in your_gridView_Name.Rows) {
DropDownList dropDown = (DropDownList)row.FindControl("dropDownList_id");
dropDown.DataSource = dataSource;
dropDown.DataValueField = "ValueField";
dropDown.DataTextField = "TextField";
dropDown.DataBind();
}
}
Please note that you have to bind GridView first, and then you have to bind your dropdown
your dropdown is in gridview so you can try with this code
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl = (DropDownList)e.Row.FindControl("EqpCatDDL'");
SqlCommand cmd = new SqlCommand("SELECT EqpCateID, EqpCat FROM EqpCategory", connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
EqpCatDDL.DataSource = ds;
EqpCatDDL.DataValueField = "EqpCateID";
EqpCatDDL.DataTextField = "EqpCat";
EqpCatDDL.DataBind();
}
}
You can't directly populate GridView's dropdownlist like this. You need to set datasource of GridView first i.e.
GridView1.DataSource = DataSource
And if you would like to access dropdownlist of this gridview, you can use RowDataBound event handler of GridView i.e.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking whether the Row is Data Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Finding the Dropdown control.
Control ctrl = e.Row.FindControl("EqpCatDDL");
if (ctrl != null)
{
DropDownList dd = ctrl as DropDownList;
List lst = new List();
dd.DataSource = lst;
dd.DataBind();
}
}
}
I am trying to set the value of ListItem Yes to PointsPossible datafield. Any insight would help. Thanks!
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="10">
<Columns>
<asp:BoundField DataField="Question" ShowHeader="False" SortExpression="Question" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlOpt1" runat="server" ShowHeader="False" AutoPostBack="false" OnSelectedIndexChanged="ddlOpt_SelectedIndexChanged">
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
<asp:ListItem Value="???">Yes</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PointsPossible" ItemStyle-HorizontalAlign="Center" HeaderText="Points Possible" ShowHeader="True" SortExpression="PointsPossible" />
</Columns>
</asp:GridView>
There are two different ways of doing this that I can think of:
Handle the OnRowDataBound event of the gridview and do the data bindings for the drop down list inside this event. Here's a step-by-step article
Add an additional SqlDataSource for the drop down list and set the the DataSourceID of the drop down list to the SqlDataSource. Here's a step-by-step article
The solution I currently have working
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl = e.Row.FindControl("ddlOpt") as DropDownList;
string val = e.Row.Cells[2].Text;
if (ddl != null)
{
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("", "");
list.Add("0", "No");
list.Add(val, "Yes");
ddl.DataSource = list;
ddl.DataTextField = "Value";
ddl.DataValueField = "Key";
ddl.DataBind();
}
}
}
I have a problem, I would like after the value from the dropdown list is picked up, inserted into the db I would like it to hide the dropdown list and just show the grade of the product that the user rated, as on the two following pictures:
This is the first picture showing how the user needs to insert the grade into the db of the product:
The result after should be as following:
The dropdownlist should now be invisible to the user which rated the product. I have tried using the RowDataBound event and the following code:
if (e.Row.RowType == DataControlRowType.DataRow)
{
hsp_Narudzbe_Detalji_Result k = (hsp_Narudzbe_Detalji_Result)e.Row.DataItem;
if (k.Ocjena!=null)
{
e.Row.Cells[4].Text = k.ocjena;
}
}
But it doesn't works, it shows the grade just once, and when I press the button for grading the product, the dropdown list is back... :/
Can someone help me out with this?
Edit (aspx code of the page):
<asp:GridView ID="gridDetaljiNarudzbe" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" runat="server" OnRowCommand="gridDetaljiNarudzbe_RowCommand" OnPageIndexChanging="gridDetaljiNarudzbe_PageIndexChanging" OnRowDataBound="gridDetaljiNarudzbe_RowDataBound">
<Columns>
<asp:BoundField DataField="Naziv" HeaderText="Naziv" />
<asp:BoundField DataField="Sifra" HeaderText="Šifra" />
<asp:BoundField DataField="Cijena" HeaderText="Cijena" />
<asp:BoundField DataField="Kolicina" HeaderText="Količina" />
<asp:TemplateField HeaderText="Ocjena">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnOcijeni" title="Ocijeni proizvod" CommandName="OcijeniCommand" CommandArgument='<%#Eval("ProizvodID") + ";" +((GridViewRow) Container).RowIndex%>' runat="server"><img src="../images/ocijeni.png" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The grades are loaded like this:
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList drop = e.Row.FindControl("DropDownList1") as DropDownList;
drop.Items.Add(new ListItem(""));
drop.Items.Add(new ListItem("1"));
drop.Items.Add(new ListItem("2"));
drop.Items.Add(new ListItem("3"));
drop.Items.Add(new ListItem("4"));
drop.Items.Add(new ListItem("5"));
}
Try something like this,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.Cells[4].FindControl("DropDownList2") as DropDownList;
if (ddl != null)
{
// if (your_condition == true)
//{
ddl .Visible = false;
//}
}
}
}
Hi Make your item template like below :
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Label ID="gvlblddlVal" runat="server" Text='<%#((YourEntityClassName)Container.DataItem).ddlVal %>'></asp:Label>
</ItemTemplate>
After that
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.Cells[4].FindControl("DropDownList2") as DropDownList;
Label lblddl = e.Row.Cells[4].FindControl("gvlblddlVal") as Label;
if (!string.IsNullOrEmpty(lblddl.Text))
{
ddl.Visible = false;
lblddl.Visible = true;
}
else
{
ddl.Visible = true;
lblddl.Visible =false;
}
}
}
Hope it helps
How can I change the value of a textbox whenever a dropdownlist within a gridview has its value changed?
On page load, the textbox shows the selected value, but when I change the selection of the dropdownlist, the textbox value doesn't change.
The code is below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:TemplateField HeaderText="Entry">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duty">
<ItemTemplate>
<asp:DropDownList ID="duty" runat="server" OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" autopostback="true" EnableViewState="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The code behind is below.
protected void ddl1_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
Duty dy = new Duty();
dt = dy.getdutyid(Convert.ToInt32(dropcontractid.SelectedValue));
DropDownList ddl = (DropDownList)sender;
ddl.DataSource = dt;
ddl.DataTextField = "dutyid";
ddl.DataValueField = "dutyid";
ddl.DataBind();
TextBox1.Text = ddl.SelectedValue;
}
}
You need to use SelectedIndexChanged handler to show selected value:
Markup:
<asp:DropDownList ID="duty" runat="server" OnLoad="ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged"></asp:DropDownList>
Code-behind:
protected void duty_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
DropDownList duty= (DropDownList) gvr.FindControl("duty");
TextBox1.Text = duty.SelectedItem.Value;
}
I had a similar problem using the DropDownLists in GridView. My solution was to adjust the onLoad for the dropdown so that it wouldn't re-write the DropDownList on every post back. This way if there's something there then it won't re-populate it.
protected void dropDownLoad(object sender, EventArgs e)
{
DropDownList dropDown = sender as DropDownList;
if (dropDown.SelectedValue == null || dropDown.SelectedValue == "")
{
// Your Code to populate table
}
}
You should look into using data binding instead. You can bind the textbox.Text to the selecteditem.value, this will ensure that proper updating takes place
this happens to me once then i code like this... but i didnt use the onLoad attribute, tell me if this works,
<asp:TemplateField HeaderText="duty" SortExpression="duty">
<EditItemTemplate>
<asp:TextBox ID="duty" runat="server" Text='<%# Bind("duty_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblduty" runat="server" Text='<%# Eval("duty_Name") %>' />
<asp:DropDownList ID="ddlduty" runat="server" CssClass="dropdownlist"
OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" Visible = "false"
>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="5%" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
I have a class named Student
public class Student
{
public string Name { get; set; }
public List<int> Marks { get; set; }
public Student()
{
}
}
and I need to bind a list of Student to GridView
List<Student> StudentList = new List<Student>();
Student stud = new Student();
stud.Name = "Scott";
List<int> marks = new List<int>();
marks.Add(10);
marks.Add(20);
marks.Add(30);
stud.Marks = marks;
StudentList.Add(stud);
Student stud1 = new Student();
stud1.Name = "Jon";
List<int> marks1 = new List<int>();
marks1.Add(10);
marks1.Add(20);
marks1.Add(30);
stud1.Marks = marks1;
StudentList.Add(stud1);
GridView1.DataSource = StudentList;
GridView1.DataBind();
The gridview shows the name field only. how I can show the list of marks also with name field. (In this all the students have same number of marks, means sometimes 3, or sometimes 5. etc.)
I need show gridview like this
Name Mark1 Mark2 Mark3
Scott 10 20 30
Jon 10 20 30
Add a custom container as TemplateField field (if number of marks varies. Example : another GridView,Repeater or ListView) or some Label or TextBox as template field(if number of marks is fixed). Override GridView1_RowDataBound and set values for the container DataSource or the labels to show the marks.
in aspx:-
<asp:GridView ID="GridView1" runat="server"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Marks">
<ItemTemplate>
<asp:DataList runat="server" ID="DataList1" RepeatDirection="Horizontal" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in cs:-
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
var student = e.Row.DataItem as Student;
var dataList = e.Row.FindControl("DataList1") as DataList;
dataList.DataSource = student.Marks;
dataList.DataBind();
}
}
Example:
<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="grid2" runat="server" AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField DataField="Mark" HeaderText="Mark" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code:
protected override void OnInit(EventArgs e)
{
grid1.RowDataBound += grid1_RowDataBound;
}
void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
var grid2 = (GridView)e.Item.FindControl("grid2");
grid2.DataSource = StudentList.Where(w => w.Name = (e.Item.DataItem as Student).Name);
grid2.Bind();
}
I do not test it
Try the following code:
protected void gridStudentDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
Student studentObj = (Student)StudentList[e.Row.RowIndex];
if (studentObj != null)
{
DropDownList ddlMarks = (DropDownList)e.Row.FindControl("ddlMarks");
if(ddlMarks != null)
{
ddlMarks.DataSource = studentObj.Marks;
ddlMarks.DataBind();
}
}
}