Click here to Skip to main content
14,981,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i am trying to write c# code or javascript for dropdownlist validation which is in gridview and also contains Add button in the footer, below is the aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="False" ShowFooter ="True" EnableModelValidation="True" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">
<columns> <asp:BoundField DataField="RowNumber" HeaderText="SNo" />
<asp:TemplateField HeaderText="Pay Element">
<itemtemplate>
<asp:DropDownList ID="drpPayElement" runat="server">

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate ="drpPayElement"
ErrorMessage="*" InitialValue="Select">

<footerstyle horizontalalign="Right">

<asp:TemplateField HeaderText="Action">
<itemtemplate>
<asp:DropDownList ID="drpAction" runat="server">
<asp:ListItem>Select
<asp:ListItem Value="G">ADD
<asp:ListItem Value="P">REPLACE

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate ="drpAction"
ErrorMessage="*" InitialValue="Select">

<footerstyle horizontalalign="Right">

<asp:TemplateField HeaderText="Amount">
<itemtemplate>
<asp:TextBox ID="txtAmount" runat="server" MaxLength="50">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAmount"
ErrorMessage="*" SetFocusOnError="True">

<footertemplate>
<asp:Button ID="ButtonAdd" runat="server" OnClick="ButtonAdd_Click" Text="Add New Row" />


<asp:CommandField ShowDeleteButton="True" />
<footerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
<rowstyle backcolor="#EFF3FB">
<editrowstyle backcolor="#2461BF">
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center">
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<alternatingrowstyle backcolor="White">




<asp:Button ID="btnSave" runat="server" Text="Save Data"/>


</form>
</body>
</html>


C# Code:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadgrid();
FirstGridViewRow();
}
}

private void FirstGridViewRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("PayElement", typeof(string)));
dt.Columns.Add(new DataColumn("Action", typeof(string)));
dt.Columns.Add(new DataColumn("Amount", typeof(string)));

dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["PayElement"] = string.Empty;
dr["Action"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);

ViewState["CurrentTable"] = dt;

GridView1.DataSource = dt;
GridView1.DataBind();

Button btnAdd = (Button)GridView1.FooterRow.Cells[3].FindControl("ButtonAdd");
Page.Form.DefaultFocus = btnAdd.ClientID;


}

public void loadgrid()
{
SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["egSQLConString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SP_PayElements", Con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ViewState["dsgetdata"] = ds;
}

private void AddNewRow()
{
int rowIndex = 0;

if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList DrpPayElement = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("drpPayElement");
DropDownList DrpAction = (DropDownList)GridView1.Rows[rowIndex].Cells[2].FindControl("drpAction");
TextBox TextBoxAmount = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtAmount");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;

dtCurrentTable.Rows[i - 1]["PayElement"] = DrpPayElement.SelectedValue;
dtCurrentTable.Rows[i - 1]["Action"] = DrpAction.SelectedValue;
dtCurrentTable.Rows[i - 1]["Amount"] = TextBoxAmount.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;

GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();

DropDownList txn = (DropDownList)GridView1.Rows[1].Cells[1].FindControl("drpPayElement");
txn.Focus();
// txn.Focus;
}
}
else
{
GridView1.Rows[0].Cells[5].Visible = false;
Response.Write("ViewState is null");
}
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList DrpPayElement = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("drpPayElement");
DropDownList DrpAction = (DropDownList)GridView1.Rows[rowIndex].Cells[2].FindControl("drpAction");
TextBox TextBoxAmount = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtAmount");

DrpPayElement.SelectedValue = dt.Rows[i]["PayElement"].ToString();
DrpAction.SelectedValue = dt.Rows[i]["Action"].ToString();
TextBoxAmount.Text = dt.Rows[i]["Amount"].ToString();
rowIndex++;
}
}
}
}

protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRow();
}
private void SetRowData()
{
int rowIndex = 0;

if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList DrpPayElement = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("drpPayElement");
DropDownList DrpAction = (DropDownList)GridView1.Rows[rowIndex].Cells[2].FindControl("drpAction");
TextBox TextBoxAmount = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtAmount");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["PayElement"] = DrpPayElement.SelectedValue;
dtCurrentTable.Rows[i - 1]["Action"] = DrpAction.SelectedValue;
dtCurrentTable.Rows[i - 1]["Amount"] = TextBoxAmount.Text;
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
//GridView1.DataSource = dtCurrentTable;
//GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//SetPreviousData();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)ViewState["dsgetdata"];
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlList = (DropDownList)e.Row.FindControl("drpPayElement");
ddlList.DataSource = ds.Tables[0];
ddlList.DataTextField = "PM_Description";
ddlList.DataValueField = "pm_Pay_Code";
ddlList.DataBind();

ddlList.Items.Insert(0, new ListItem("--Select--", "0"));

}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SetRowData();
if (ViewState["CurrentTable"] != null)
{
DataTable Dt = new DataTable();
DataRow Drcurrentdata = null;
Dt = (DataTable)ViewState["CurrentTable"];
int rowIndex = Convert.ToInt32(e.RowIndex);
if (Dt.Rows.Count > 1)
{
Dt.Rows.Remove(Dt.Rows[rowIndex]);
Drcurrentdata = Dt.NewRow();
ViewState["Currenttable"]=Dt;
GridView1.DataSource = Dt;
GridView1.DataBind();

for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
GridView1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
}
SetPreviousData();
}
}
}
}

Here in the gridview 1 dropdownlist and a textbox inside, were in am binding data to dropdownlist from database. In the gridview
once i select the value from dropdown and then i click new button, if i again select same value and click add New button it should through message

"A value alredy selected".

Please give me any sample code.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900