Click here to Skip to main content
15,884,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class Default2 : System.Web.UI.Page
{
List<string> list = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
//BindData();
if (!IsPostBack)
{
selectFeatureType();
AddDefaultFirstRecord();
}
}

protected void selectFeatureType()
{
FeatureTypeRepository type = new FeatureTypeRepository();
IList<featuretype> FeatureType = type.GetAllFeatureType();
cmbFeatureType.DataSource = from m in FeatureType
select new { m.featureType, m.feature_type_id };
cmbFeatureType.DataTextField = "featureType";
cmbFeatureType.DataValueField = "feature_type_id";
cmbFeatureType.DataBind();

}

protected void cmbFeatureType_SelectedIndexChanged1(object sender, EventArgs e)
{
int id = Convert.ToInt32(cmbFeatureType.SelectedValue);
FeatureDescriptionRepository desc = new FeatureDescriptionRepository();
IList<featuredescription> FeatureDesc = desc.GetAllFeatureDescription();
cmbDescription.DataSource = from m in FeatureDesc
where m.feature_type_id == id
select new { m.description ,m.description_id };
cmbDescription.DataTextField = "description";
cmbDescription.DataValueField = "description_id";
cmbDescription.DataBind();

TextBox1.Text = cmbFeatureType.SelectedValue;
}


private void AddDefaultFirstRecord()
{
//creating dataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "Feature";
dt.Columns.Add(new DataColumn("feature_type_id", typeof(int)));
dt.Columns.Add(new DataColumn("description_id", typeof(int)));
dt.Columns.Add(new DataColumn("FeatureType", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
dr = dt.NewRow();
dt.Rows.Add(dr);
//saving databale into viewstate
ViewState["Feature"] = dt;
//bind Gridview
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void btnAdd_Click(object sender, EventArgs e)
{
//CheckDuplicateFeature();
AddNewVehicleFeatureToGrid();
}
private void CheckDuplicateFeature()
{
// string bS = Convert.ToString(GridView1.Rows[rowIndex].Cells[2].Text);

// ClientScript.RegisterStartupScript(this.GetType(), "Success", "<script type='text/javascript'>alert('Cannot add same vehicle type!');window.location='Default2.aspx';</script>'");

}
private void AddNewVehicleFeatureToGrid()
{
if (ViewState["Feature"] != null)
{

//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["Feature"];
DataRow drCurrentRow = null;

if (dtCurrentTable.Rows.Count > 0)
{

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//add each row into data table

drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["feature_type_id"] = cmbFeatureType.SelectedValue;
drCurrentRow["FeatureType"] = cmbFeatureType.SelectedItem.Text;
drCurrentRow["description_id"] = cmbDescription.SelectedValue;
drCurrentRow["Description"] = cmbDescription.SelectedItem.Text;
}
//Remove initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}

//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Save Data table into view state after creating each row
ViewState["Feature"] = dtCurrentTable;

//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();



int rowscount = GridView1.Rows.Count;
string id = cmbFeatureType.SelectedValue;
foreach (GridViewRow item in GridView1.Rows)
{
list.Add(item.Cells[0].Text);
// for (int i = 0; i < rowscount; i++)
// {
// list.Add(GridView1.Rows[i].Cells[0].Text);
// string bS = Convert.ToString(GridView1.Rows[1].Cells[0].Text);
//if (GridView1.Rows[i].Cells[0].Text == id)
//{
// TextBox1.Text = "Data already exist";
//}
if (list.Contains(id))
{
TextBox1.Text = "Data Already Exist";
}

}

}
}
}
}
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