Hey everyone!
Basically, I am trying to load data dynamically into a ListBox (works fine).
Then, I want to be able to select multiple items from the list box, and display these in a Gridview (not working fine).
Please help me. Thanks in advance.
Loading
DhanashriPosted Oct 25, 2013, 2:16 AM
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class About : System.Web.UI.Page
{
private String strConnection = "Data Source=.\\SQLEXPRESS;Initial Catalog=INDHS;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable("PatAllergy");
DataRow dr = null;
dt.Columns.Add(new DataColumn("AllegType", typeof(string)));
dt.Columns.Add(new DataColumn("AllgName", typeof(string)));
dr = dt.NewRow();
dr["AllegType"] = string.Empty;
dr["AllgName"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["PatAllergy"] = dt;
Gridview1.DataSourceID = null;
Gridview1.DataSource = dt;
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (Gridview1.Rows.Count > 0)
{
DataTable dt1 = new DataTable("PatAllergy");
DataTable dt = (DataTable)ViewState["PatAllergy"];
DataRow dr = null;
if (dt.Rows.Count > 0)
{
for (int i = 1; i <= dt.Rows.Count; i++)
{
dr = dt.NewRow();
dr["AllegType"] = Gridview1.Rows[rowIndex].Cells[0].FindControl("Allergy Type");
dr["AllgName"] = Gridview1.Rows[rowIndex].Cells[1].FindControl("Allergy Name");
rowIndex++;
}
//add new row to DataTable
dt.Rows.Add(dr);
//Store the current data to ViewState
ViewState["PatAllergy"] = dt;
//Rebind the Grid with the current data
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["PatAllergy"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["PatAllergy"];
DataRow drPrevRow;
DataTable dt = (DataTable)ViewState["PatAllergy"];
if (dt.Rows.Count > 0)
{
for (int i = 1; i < dt.Rows.Count; i++)
{
drPrevRow = dtCurrentTable.NewRow();
drPrevRow["AllegType"] = Gridview1.Rows[rowIndex].Cells[0].FindControl("AllegType");
drPrevRow["AllgName"] = Gridview1.Rows[rowIndex].Cells[1].FindControl("AllgName");
drPrevRow["AllegType"] = dt.Rows[i]["AllegType"].ToString();
drPrevRow["AllgName"] = dt.Rows[i]["AllgName"].ToString();
rowIndex++;
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void lstAllergy_SelectedIndexChanged(object sender, EventArgs e)
{
// int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select AllgName from PatAllergy where AllegType='" + lstAllergy.SelectedItem.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
lstAllgyItem.DataSource = ds;
lstAllgyItem.DataTextField = "AllgName";
lstAllgyItem.DataBind();
}
}
Jaganathan BantheswaranPosted Oct 25, 2013, 2:10 AM