TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
lalitha
NA
11
0
gridview-edit
Feb 2 2011 6:02 PM
i have a gridview with a edit button. when selected i have a dropdownlist user the courseidwhich shows all the courselist, and a label in the coursename such that when ever the course id is selected from the list the corresponding course name should be displayed. i am using item/edit template field.
code:
<asp:GridView ID="GridView2" DataKeyNames="site_id" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial" Width = "550px"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "skyblue"
AllowPaging ="true" ShowFooter = "true"
OnPageIndexChanging = "OnPaging" onrowediting="EditCustomer"
onrowupdating="UpdateCustomer" onrowcancelingedit="CancelEdit"
PageSize = "10" >
<Columns>
<asp:TemplateField Visible="false" ItemStyle-Width = "30px" HeaderText = "Course_id">
<ItemTemplate>
<asp:Label ID="lblCourse_id" runat="server"
Text='<%# Eval("Course_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "Course_no">
<ItemTemplate>
<asp:Label ID="lblCourse_no" runat="server"
Text='<%# Eval("Course_no")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_course_no" AutoPostBack="true" OnSelectedIndexChanged="ddl_courseno_SelectedIndexChanged" Width="100px" runat="server" DataTextField ="Course_no" DataValueField ="Course_id" DataSourceID ="SqlDataSource1" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "Title">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server"
Text='<%# Eval("Title")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbl_cname" runat="server">abc</asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false" ItemStyle-Width = "30px" HeaderText = "site_id">
<ItemTemplate>
<asp:Label ID="lblsite_id" runat="server"
Text='<%# Eval("site_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "50px" HeaderText = "Site_No">
<ItemTemplate>
<asp:Label ID="lblsite_no" runat="server"
Text='<%# Eval("site_no")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "150px" HeaderText = "Site Name">
<ItemTemplate>
<asp:Label ID="lblsite_name" runat="server"
Text='<%# Eval("site_name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Rotation">
<ItemTemplate>
<asp:Label ID="lbldisplay" runat="server"
Text='<%# Eval("display")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Max Student">
<ItemTemplate>
<asp:Label ID="lblmax_students" runat="server"
Text='<%# Eval("max_students")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<AlternatingRowStyle BackColor="#63B2EB" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:sitereg_conn %>"
SelectCommand="SELECT [Course_id], [Course_no] FROM [Course_table]">
</asp:SqlDataSource>
</div>
c#code:
SQLLogic dl = new SQLLogic();
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
// if(true)
{
BindData();
}
}
catch (Exception ex)
{
err_Label.Text = (ex.Message);
}
}
private void BindData()
{
//string strSql = "select course_table.course_id, course_table.course_no,course_table.title,siteinfo_table.site_id,siteinfo_table.site_no,siteinfo_table.site_name,siteinfo_table.course_time,siteinfo_table.max_students from course_table, siteinfo_table where course_table.course_id = siteinfo_table.course_id";
string strSql = "" +
" select " +
" course_table.course_id " +
" , course_table.course_no " +
" ,course_table.title " +
" ,siteinfo_table.site_id " +
" ,siteinfo_table.site_no " +
" ,siteinfo_table.site_name " +
" ,b.display" +
" ,siteinfo_table.max_students " +
" from course_table, siteinfo_table " +
" join Rotation b " +
" on b.code=siteinfo_table.Rotation " +
" where course_table.course_id = siteinfo_table.course_id ";
DataSet ds = dl.CSRMyExecuterQuery(strSql);
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
//((DropDownList)Page.FindControl("ddl1")).DataSource = coursesarray;
//((DropDownList)Page.FindControl("ddl1")).DataBind();
}
protected void EditCustomer(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
BindData();
}
protected void ddl_courseno_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
err_Label.Visible = true;
err_Label.Text = "ddl selectindexchange method run";
DropDownList ddl1 = (DropDownList)sender;
int RowIndex = ((GridViewRow)ddl1.NamingContainer).RowIndex;
string site_id = GridView2.DataKeys[RowIndex].Value.ToString();
string strSql = "select Title from Course_table where Course_id = '" + ddl1.SelectedValue.ToString() + "'";
DataSet ds = dl.CSRMyExecuterQuery(strSql);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
err_Label.Text = dr["Title"].ToString();
//
how do i assign the coursename here????????
}
}
else
{
err_Label.Text = "No Details Avaliable";
}
}
catch (Exception ex)
{
err_Label.Text = ex.Message;
}
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex = -1;
BindData();
}
protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
{
string course_id = ((DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddl_course_no")).SelectedValue.ToString();
}
int i = -1;
// string str = "update siteinfo_table set coures_id=" + course_id.ToString() + " where site_id='" + site_id + "'";
// i = dl.CSRExecuteNonQuery(str);
i = 1;
if (i >= 0)
{
err_Label.Visible = true;
err_Label.Text = "updated";
}
string strSql1 = " select course_table.course_id, course_table.course_no, course_table.title, siteinfo_table.site_id,siteinfo_table.site_no, siteinfo_table.site_name,b.display,siteinfo_table.max_students from course_table, siteinfo_table join Rotation b on b.code=siteinfo_table.Rotation where course_table.course_id = siteinfo_table.course_id";
DataSet ds1 = dl.CSRMyExecuterQuery(strSql1);
GridView2.EditIndex = -1;
GridView2.DataSource = ds1.Tables[0];
GridView2.DataBind();
// BindData();
}
Reply
Answers (
2
)
multilanguage site
dropdownlist in gridview