I have a class that I did all calculation in it and now I want to represent this data in datagridview what should I do using c#?
that what I did but it didn't work
for (int i = 1; i <= cust_num; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["Customer_Number"].Value = cust_num.ToString();
dataGridView1.Rows[i].Cells["Time_between_Arrival"].Value = inter_Arrival_time.ToString();
dataGridView1.Rows[i].Cells["service_Time"].Value = service_Time.ToString();
dataGridView1.Rows[i].Cells["Arrival_Time"].Value = rand_inter_Arrival.ToString();
dataGridView1.Rows[i].Cells["Time_Service_Beg"].Value = Arrival_Time.ToString();
dataGridView1.Rows[i].Cells["ServiceNum"].Value = service_number.ToString();
dataGridView1.Rows[i].Cells["service_Time"].Value = rand_service_time.ToString();
dataGridView1.Rows[i].Cells["Time_service_End"].Value = finsh_time.ToString();
dataGridView1.Rows[i].Cells["Waiting_Time"].Value = waiting_time.ToString();
// dataGridView1.Rows.Add(row);
}
string[] row = new string[] { cust_num.ToString(), inter_Arrival_time.ToString(), ...... };
dataGridView1.Rows.Add(row);
You can add row to datagridview like this
Related
I have a grid view with multiple columns which allow user to fill in the data and they are able to add a new row after finishing filling the data. Among the columns, there is a column with CheckBoxList which I allow user to multiple select the option on the CheckBoxList but every time add a new row, only the first option select by the user is remain while other selection is gone. How am I able to let the option selected by the user remain while I add a new row?
private void SetPreviousDataLecturer()
{
int rowIndex = 0;
if (ViewState["LecturerGridView"] != null)
{
DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"];
if (dataTableCurrent.Rows.Count > 0)
{
for (int i = 0; i < dataTableCurrent.Rows.Count; i++)
{
TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName");
TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID");
TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress");
TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber");
TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress");
CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse");
TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword");
LecturerGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
textBoxLName.Text = dataTableCurrent.Rows[i]["LecturerName"].ToString();
textBoxLID.Text = dataTableCurrent.Rows[i]["LecturerID"].ToString();
textBoxLAdd.Text = dataTableCurrent.Rows[i]["LecturerAddress"].ToString();
textBoxLPNumber.Text = dataTableCurrent.Rows[i]["LecturerPNumber"].ToString();
textBoxLEAdd.Text = dataTableCurrent.Rows[i]["LecturerEAddress"].ToString();
checkBoxListLCourse.SelectedValue = dataTableCurrent.Rows[i]["LecturerCourse"].ToString();
textBoxLPassword.Text = dataTableCurrent.Rows[i]["LecturerPassword"].ToString();
rowIndex++;
}
}
}
}
private void AddNewRowToLecturerGV()
{
int rowIndex = 0;
if (ViewState["LecturerGridView"] != null)
{
DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"];
DataRow dataRowCurrent = null;
if (dataTableCurrent.Rows.Count > 0)
{
for (int i = 1; i <= dataTableCurrent.Rows.Count; i++)
{
TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName");
TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID");
TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress");
TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber");
TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress");
CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse");
TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword");
dataRowCurrent = dataTableCurrent.NewRow();
dataRowCurrent["RowNumber"] = i + 1;
dataTableCurrent.Rows[i - 1]["LecturerName"] = textBoxLName.Text;
dataTableCurrent.Rows[i - 1]["LecturerID"] = textBoxLID.Text;
dataTableCurrent.Rows[i - 1]["LecturerAddress"] = textBoxLAdd.Text;
dataTableCurrent.Rows[i - 1]["LecturerPNumber"] = textBoxLPNumber.Text;
dataTableCurrent.Rows[i - 1]["LecturerEAddress"] = textBoxLEAdd.Text;
dataTableCurrent.Rows[i - 1]["LecturerCourse"] = checkBoxListLCourse.SelectedValue.ToString();
dataTableCurrent.Rows[i - 1]["LecturerPassword"] = textBoxLPassword.Text;
rowIndex++;
}
dataTableCurrent.Rows.Add(dataRowCurrent);
ViewState["LecturerGridView"] = dataTableCurrent;
LecturerGridView.DataSource = dataTableCurrent;
LecturerGridView.DataBind();
}
}
else
{
Response.Write("ViewState is null.");
}
SetPreviousDataLecturer();
}
You need to maintain state for selected checkbox. On the button click 'Add new row' first get the state of each rows in a DataTable and add a blank row then populate that DataTable.
You need to maintain checkbox's selected item's state also. You can get selected values in a CSV as :
string selectedItems = String.Join(",",
checkBoxListLCourse.Items.OfType<ListItem>().Where(r => r.Selected)
.Select(r => r.Value));
and you can restore as :
string[] items = selectedItems.Split(',');
for (int i = 0; i < checkBoxListLCourse.Items.Count; i++)
{
if (items.Contains(checkBoxListLCourse.Items[i].Value))
{
checkBoxListLCourse.Items[i].Selected = true;
}
}
My answer. This answer has some problem like the checkbox list will automatically scroll to the most top when we tick on anything in the checkbox list.
private void SetPreviousDataLecturer()
{
int rowIndex = 0;
if (ViewState["LecturerGridView"] != null)
{
DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"];
if (dataTableCurrent.Rows.Count > 0)
{
for (int i = 0; i < dataTableCurrent.Rows.Count; i++)
{
TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName");
TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID");
TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress");
TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber");
TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress");
CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse");
TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword");
LecturerGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
textBoxLName.Text = dataTableCurrent.Rows[i]["LecturerName"].ToString();
textBoxLID.Text = dataTableCurrent.Rows[i]["LecturerID"].ToString();
textBoxLAdd.Text = dataTableCurrent.Rows[i]["LecturerAddress"].ToString();
textBoxLPNumber.Text = dataTableCurrent.Rows[i]["LecturerPNumber"].ToString();
textBoxLEAdd.Text = dataTableCurrent.Rows[i]["LecturerEAddress"].ToString();
checkBoxListLCourse.Text = dataTableCurrent.Rows[i]["LecturerCourse"].ToString();
string lecturerCourse = dataTableCurrent.Rows[i]["LecturerCourse"].ToString();
if (!string.IsNullOrEmpty(lecturerCourse))
{
for (int j = 0; j < lecturerCourse.Split(',').Length; j++)
{
checkBoxListLCourse.Items.FindByValue(lecturerCourse.Split(',')[j].ToString()).Selected = true;
}
}
textBoxLPassword.Text = dataTableCurrent.Rows[i]["LecturerPassword"].ToString();
rowIndex++;
}
}
}
}
private void AddNewRowToLecturerGV()
{
int rowIndex = 0;
if (ViewState["LecturerGridView"] != null)
{
DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"];
DataRow dataRowCurrent = null;
if (dataTableCurrent.Rows.Count > 0)
{
for (int i = 1; i <= dataTableCurrent.Rows.Count; i++)
{
TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName");
TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID");
TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress");
TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber");
TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress");
CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse");
TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword");
dataRowCurrent = dataTableCurrent.NewRow();
dataRowCurrent["RowNumber"] = i + 1;
dataTableCurrent.Rows[i - 1]["LecturerName"] = textBoxLName.Text;
dataTableCurrent.Rows[i - 1]["LecturerID"] = textBoxLID.Text;
dataTableCurrent.Rows[i - 1]["LecturerAddress"] = textBoxLAdd.Text;
dataTableCurrent.Rows[i - 1]["LecturerPNumber"] = textBoxLPNumber.Text;
dataTableCurrent.Rows[i - 1]["LecturerEAddress"] = textBoxLEAdd.Text;
string lecturerCourse = string.Empty;
foreach (ListItem item in checkBoxListLCourse.Items)
{
if (item.Selected)
{
if (!string.IsNullOrEmpty(lecturerCourse))
{
lecturerCourse += ",";
}
lecturerCourse += item.Value;
}
}
dataTableCurrent.Rows[i - 1]["LecturerCourse"] = lecturerCourse;
dataTableCurrent.Rows[i - 1]["LecturerPassword"] = textBoxLPassword.Text;
rowIndex++;
}
dataTableCurrent.Rows.Add(dataRowCurrent);
ViewState["LecturerGridView"] = dataTableCurrent;
LecturerGridView.DataSource = dataTableCurrent;
LecturerGridView.DataBind();
}
}
else
{
Response.Write("ViewState is null.");
}
SetPreviousDataLecturer();
}
I am using SQLite to store the data for my application.
The application has a UI that loads the data from the SQLite database to display it table by table to the user. Basically the user can click left or right and view the other tables one by one.
The user can also click a button that will show a print preview of the data and let them print it. I have this working, but I am having some issues devising a dynamic way to display ANY table on the print preview screen perfectly. My concerns are if some column titles are too long etc, basically how to calculate the size of each column etc. Should I loop through and find the max character size of any text in the entire column and then set the column width to just wider than that or is there an easier way to do this?
I also write the data-table to a comma separated csv file so it might be a better alternative to use an existing solution to print from a csv file if any of you know such a solution then please suggest it!
Anyway here is the currently existing code:
// ------------------------ code that gets called when the print button is clicked ----------------------------
// holds the row data
List<string[]> myList = new List<string[]>();
if(ReportPage == 1)
{
int rowCount = MyTestTable.RowCount;
for(int i = 0; i <rowCount; i++)
{
MyTestTable.SelectedRowIndex = i;
var row1 = MyTestTable.GetSelectedRow();
var cols1 = row1.ItemArray;
string Col1 = cols1[row1.FindIndexOfColumn("Col1")].ToString();
string Col2 = cols1[row1.FindIndexOfColumn("Col2")].ToString();
string Col3 = cols1[row1.FindIndexOfColumn("Col3")].ToString();
string Col4 = cols1[row1.FindIndexOfColumn("Col4")].ToString();
string Col5 = cols1[row1.FindIndexOfColumn("Col5")].ToString();
string Col6 = cols1[row1.FindIndexOfColumn("Col6")].ToString();
string Col7 = cols1[row1.FindIndexOfColumn("Col7")].ToString();
myList.Add(new string[] { Col1, Col2, Col3, Col4, Col5, Col6, Col7 });
}
string[] cols = new string[7];
cols[0] = "Col1";
cols[1] = "Col2";
cols[2] = "Col3";
cols[3] = "Col4";
cols[4] = "Col5";
cols[5] = "Col6";
cols[6] = "Col7";
PrintUtility.SetUpDocument("TEST", cols, myList);
}
PrintUtility.TestNewReport();
// ---------------------- plugin code that gets called from the application
namespace PrintUtility
{
public class PrintUtility : UserComponentBase
{
public PrintDocument document;
public DataGridView dataGridView;
public PrintUtility()
{
document = new PrintDocument();
dataGridView = new DataGridView();
}
public void SetUpDocument(string title, string[] cols, List<string[]> rows)
{
document = new PrintDocument();
dataGridView = new DataGridView();
document.DocumentName = title;
document.DefaultPageSettings.Landscape = true;
document.PrintPage += PrintPage;
DataGridView dataGrid = new DataGridView();
dataGrid.ColumnCount = cols.Length;
for (int i = 0; i < cols.Length; i++ )
{
dataGrid.Columns[i].Name = cols[i];
}
foreach(string[] row in rows)
{
dataGrid.Rows.Add(row);
}
this.dataGridView = dataGrid;
document.DocumentName = title;
document.PrintPage += PrintPage;
}
public PrintDocument GetDocument()
{
return this.document;
}
private DataTable ConvertListToDataTable(List<string[]> list)
{
// New table.
DataTable table = new DataTable();
// Get max columns.
int columns = 0;
foreach (var array in list)
{
if (array.Length > columns)
{
columns = array.Length;
}
}
// Add columns.
for (int i = 0; i < columns; i++)
{
table.Columns.Add();
}
// Add rows.
foreach (var array in list)
{
table.Rows.Add(array);
}
return table;
}
public void TestNewReport()
{
Report report = new Report(new PdfFormatter());
FontDef fd = new FontDef(report, "Helvetica");
FontProp fp = new FontPropMM(fd, 4);
FontProp fp_Title = new FontPropMM(fd, 6);
FontProp fp_Word = new FontPropMM(fd, 3);
fp_Title.bBold = true;
List<string> columns = new List<string>();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
columns.Add(column.Name.ToString());
}
List<List<string>> rows = new List<List<string>>();
foreach (DataGridViewRow row in dataGridView.Rows)
{
List<string> rowSingle = new List<string>();
foreach (DataGridViewCell cell in row.Cells)
{
try
{
rowSingle.Add(cell.Value.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
rows.Add(rowSingle);
}
// AUDIT TABLE ( This way of doing things is not dynamic )
Page page = new Page(report);
page.SetLandscape();
int x = 10;
int y = 40;
int pastLength = 0;
foreach(string col in columns)
{
x += ((pastLength * 14) + 31);
page.Add(x, y, new RepString(fp_Title, col));
pastLength = col.Length;
}
page.Add(0, 52, new RepString(fp_Title, "_________________________________________________________________"));
/* Dynamic way starting
int rowX = 10;
int rowY = 105;
foreach (List<string> row in rows)
{
int pastLength2 = 0;
foreach (string rowItem in row)
{
page.Add(rowX, rowY, new RepString(fp_Word, rowItem));
rowX += ((pastLength * 14) + 31);
pastLength2 = rowItem.Length;
}
rowY += 30;
}
*/
fp_Title.rSizeMM = 8;
int amountRowsPerPageSoFar = 0;
int rowY = 80;
foreach (List<string> row in rows)
{
try
{
string iDItem = row[0];
page.Add(40, rowY, new RepString(fp_Word, iDItem));
string typeItem = row[1];
page.Add(120, rowY, new RepString(fp_Word, typeItem));
string descriptionItem = row[2];
page.Add(190, rowY, new RepString(fp_Word, descriptionItem));
string timestampItem = row[3];
page.Add(375, rowY, new RepString(fp_Word, timestampItem));
string userItem = row[4];
page.Add(555, rowY, new RepString(fp_Word, userItem));
string stationItem = row[5];
page.Add(655, rowY, new RepString(fp_Word, stationItem));
string activeItem = row[6];
page.Add(775, rowY, new RepString(fp_Word, activeItem));
amountRowsPerPageSoFar++;
rowY += 30;
if (amountRowsPerPageSoFar >= 17)
{
page = new Page(report);
page.SetLandscape();
amountRowsPerPageSoFar = 0;
rowY = 40;
}
}
catch (Exception ex)
{
}
}
RT.ViewPDF(report, "TestReport.pdf");
}
}
}
I have a data table in a data grid view. I want to put an listbox/dropdown menu in a specific column and row. I have tried this but it doesn't work --
var column = new DataGridViewComboBoxColumn();
RunTimeCreatedDataGridView[1, 1].Value = RunTimeCreatedDataGridView.Columns.Add(column);
Here is how I populate the table--
public DataTable createGridForForm(int rows, int columns)
{
// Create the output table.
DataTable table = new DataTable();
for (int i = 1; i <= columns; i++)
{
table.Columns.Add("column " + i.ToString());
}
for (int i = 1; i < rows; i++)
{
DataRow dr = table.NewRow();
// populate data row with values here
ListBox test = new ListBox();
myTabPage.Controls.Add(test);
table.Rows.Add(dr);
}
return table;
}
And here is how I create the datagridview.
private void createGridInForm(int rows, int columns)
{
DataGridView RunTimeCreatedDataGridView = new DataGridView();
RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
//DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
//ID_Column.Width = 200;
int positionForTable = getLocationForTable();
RunTimeCreatedDataGridView.BackgroundColor = Color.WhiteSmoke;
RunTimeCreatedDataGridView.Size = new Size(995, 200);
RunTimeCreatedDataGridView.Location = new Point(5, positionForTable);
myTabPage.Controls.Add(RunTimeCreatedDataGridView);
}
You were right on track with your first code block... the problem was you were trying to add a column to a cell reference. If you want to add a column where all rows have a drop down, then do exactly what you were doing, but simply add the column instead of specifying a cell value, like this:
var column = new DataGridViewComboBoxColumn();
RunTimeCreatedDataGridView.Columns.Add(column);
Then, specify the datasource as you normally would for a combobox.
Alternatively, if you want a specific cell to have a different combobox, or only a single cell in the column to have one, you can create a separate one as shown here or here.
Edit: To add a combobox to a specific cell in the DataGridView, you would do something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataGridView dgv = new DataGridView();
// add some columns and rows
for (int i = 0; i < 10; i++)
{
DataGridViewCell c = new DataGridViewHeaderCell();
dgv.Columns.Add("Column" + i, Convert.ToString(i));
}
for (int i = 0; i < 10; i++)
{
dgv.Rows.Add(new DataGridViewRow());
}
//create a new DataGridViewComboBoxCell and give it a datasource
var DGVComboBox = new DataGridViewComboBoxCell();
DGVComboBox.DataSource = new List<string> {"one", "two", "three"};
DGVComboBox.Value = "one"; // set default value of the combobox
// add it to cell[4,4] of the DataGridView
dgv[4, 4] = DGVComboBox;
// add the DataGridView to the form
this.Controls.Add(dgv);
}
}
Im new to C# and want to use a datagridview where the two first colums is the same every time and the rest i dependent of the content of a string list. But how can i do that ?
My code looks like this right now and i can see the failure, but dont know what to do about it :(
Plz help me :)
View.Columns.Clear();
View.AutoGenerateColumns = false;
DataGridViewTextBoxColumn colTime = new DataGridViewTextBoxColumn();
colTime.DataPropertyName = "Time";
colTime.HeaderText = "Time";
colTime.Name = "Time";
colTime.Width = 55;
View.Columns.Add(colTime);
DataGridViewTextBoxColumn colPlace = new DataGridViewTextBoxColumn();
colPlace.DataPropertyName = "Place";
colPlace.HeaderText = "Place";
colPlace.Name = "Place";
colPlace.Width = 55;
View.Columns.Add(colPlace);
for(int i = 0; i < Liste.Length; i++)
{
DataGridViewCheckBoxColumn "Log"+List[i] = new DataGridViewCheckBoxColumn();
"Log"+List[i].DataPropertyName = "List[i]";
"Log"+List[i].HeaderText = "List[i]";
"Log"+List[i].Name = "List[i]";
"Log"+List[i].Width = 55;
View.Columns.Add("Log"+List[i]);
}
Thanks in advance
you can use this function for creating typical DataGridView automatically,
DataGridView CreateDataGridDynamically(int NumberOfCol)
{
DataGridView D = new DataGridView();
for (int i = 0; i < NumberOfCol; i++)
{
DataGridViewTextBoxColumn C1 = new DataGridViewTextBoxColumn();
C1.HeaderText = "Something";
D.Columns.Add(C1);
}
return D;
}
and with datagridview.Rows.Add() and the forth overload, you can simply add some rows with your constraint:
DataGridView D = CreateDataGridDynamically(3);
D.Rows.Add("Sth1", "Sth1", "Sth3");
D.Rows.Add("Sth1", "Sth1", "Sth3");
D.Rows.Add("Sth1", "Sth1", "Sth3");
Controls.Add(D);
This is my sorting logic alphabetic wise
string selectedVal = lstSelectionTags.SelectedValue;
SortedList sortedItems = new SortedList();
for (int i = 0; i < lstSelectionTags.Items.Count; i++)
{
sortedItems.Add(lstSelectionTags.Items[i].Text, lstSelectionTags.Items[i].Value);
}
lstSelectionTags.Items.Clear();
lstSelectionTags.DataSource = sortedItems;
lstSelectionTags.DataTextField = "key";
lstSelectionTags.DataValueField = "value";
lstSelectionTags.DataBind();
when i display items first time in my Listbox by using the below give code
string valueField = Convert.ToString(lstSelectionSub.SelectedItem);
int catID = Convert.ToInt32(lstSelectionSub.SelectedValue);
util = new Utilities();
dt1 = util.GetSubTags_PD(catID, false);
string[] lines = new string[100];
List<string> lines1 = new List<string>();
for (int i = 0; i < dt1.Rows.Count; i++)
{
string s1 = dt1.Rows[i][0].ToString();
if (s1 != "")
{
lines = Regex.Split(s1, ",");
if (!lines1.Contains(lines.ToString()))
{
lines1.AddRange(lines);
}
}
}
lstSelectionTags.DataSource = lines1.Distinct();
lstSelectionTags.DataBind();
It works fine initially and displays the Data but when i do sorting and then try and access the values i don't get any value in the ListBox
EDIT: There seems to be some issue with sorting after sorting the Listbox has a key and value as the DataValue and DataText Field whereas when i rebind it there is no DataValue and DataText Field. Please help.
ISSUE SOLVED : Just used
if (lines1.Count > 0)
{
lstSelectionTags.DataSource = null;
lstSelectionTags.Items.Clear();
lstSelectionTags.DataSource = lines1.Distinct();
lstSelectionTags.DataTextField = null;
lstSelectionTags.DataValueField = null;
lstSelectionTags.DataBind();
}