I am trying to read my checkbox values in my table cells, however while doing a postback via button submit, the entire table disappears. I only create the table if it's not a postback when a page_load occurs and I thought the table would persist across postback once created.
How do I retain the entire table with its cell's checkboxes values? Thanks.
protected void CreateTable()
{
int rowCnt; // Total number of rows.
int rowCtr; // Current row count.
int cellCtr; // Total number of cells per row (columns).
int cellCnt; // Current cell counter.
rowCnt = 6;
cellCnt = 8;
string baseStartTime = (ConfigurationManager.AppSettings["DEFAULTBASESELLSCHEDULETIME"]);
int incrementInMins = Convert.ToInt32((ConfigurationManager.AppSettings["DEFAULTBASESELLSCHEDULETIME_INCREMENT"]));
DateTime tempTimeFrom = Convert.ToDateTime(baseStartTime); // Converts only the time
tempTimeFrom = tempTimeFrom.AddMinutes(-incrementInMins);
// Because the very first loop will add 30 mins right away
for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
{
tempTimeFrom = tempTimeFrom.AddMinutes(incrementInMins);
DateTime tempTimeTo = tempTimeFrom.AddMinutes(incrementInMins);
string timeFrom = tempTimeFrom.ToString("hh:mm tt");
string timeToClassName = tempTimeTo.ToString("hh:mm");
string timeTo = tempTimeTo.ToString("hh:mm tt");
// Create a new row and add it to the table.
TableRow tRow = new TableRow();
tblSellSchedule.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
tRow.Cells.Add(tCell);
if (cellCtr == 1) // We need the time for the first column of every row
{
tCell.Controls.Add(new LiteralControl(timeFrom + "-" + timeTo));
tCell.CssClass = timeToClassName;
}
else
{
// tCell.Controls.Add(new LiteralControl("Select"));
CheckBox chkbox = new CheckBox();
chkbox.ID = tblSellSchedule.Rows[rowCtr - 1].Cells[0].CssClass + (cellCtr - 1);
tCell.Controls.Add(chkbox);
// tCell.ID = (cellCtr - 1).ToString();
tCell.CssClass = (cellCtr - 1).ToString();
}
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (TableRow row in tblSellSchedule.Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (CheckBox c in cell.Controls.OfType<CheckBox>())
{
if (c.Checked)
{
var idVal = c.ID;
}
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateTable();
}
}
I think you should call your method CreateTable() in page_Init().
because on every post back every DOM content you have created will be vanished. so you have to create it again on every post back, so you have to do this recreation in page_Init() that is accessed before page_Load().
Related
So I have a gridview and I wanted to make certain columns text a different colour... i.e every column that says actual I want this text to be green... can anybody help? My gridlooks similar to this.
Hour - actual A - target A - actual aa - target aa - actual b - target b.
And finally is there a way to reset the data in my gridview after a certain amount of time... i.e shiftstart 6am-2pm 2pm-10pm 10pm-6am... So the data refreshes after 8 hours back to zero.
public void Refreshdata(int selectedProduct, DateTime shiftStart, DateTime shiftEnd)
{
BizManager biz = new BizManager();
GridView1.DataSource = biz.GetPacktstatisticsForShift(
shiftStart
, shiftEnd
, selectedProduct).DefaultView;
GridView1.DataBind();
public DataTable CreatePackingStats(DataSet dset)
{
using (DataManager dmgr = new DataManager())
{
DataTable target = dset.Tables[0];
DataTable actual = dset.Tables[1];
DataColumn[] cols = new DataColumn[1];
cols[0] = actual.Columns["Hour"];
actual.PrimaryKey = cols;
DataTable final = new DataTable();
// Create table columns
foreach (DataColumn col in target.Columns)
{
final.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
if (col.ColumnName.Contains("Target"))
{
// Add an equivilant actual column
string newColumnName = col.ColumnName.Replace("Target", "Actual");
final.Columns.Add(newColumnName, col.DataType);
}
}
//// Add rows to new table
foreach (DataRow row in target.Rows)
{
string key = row["Hour"].ToString();
DataRow newRow = final.Rows.Add();
// Store column value
foreach (DataColumn col in final.Columns)
{
if (col.ColumnName.Contains("HOUR") || col.ColumnName.Contains("Target"))
{
newRow[col.ColumnName] = row[col.ColumnName];
}
else
{
// Find actual data
DataColumn actColumn = actual.Columns[col.ColumnName] as DataColumn;
if (actColumn == null)
{
newRow[col.ColumnName] = 0;
}
else
{
if (string.IsNullOrEmpty(actual.Rows.Find(key)[col.ColumnName].ToString()))
{
newRow[col.ColumnName] = 0;
}
else
{
newRow[col.ColumnName] = actual.Rows.Find(key)[col.ColumnName].ToString();
}
}
}
}
}
return final;
The CreatePackingStats is populating my grid with added columns FYI.
I guess there is a way to add colour text whilst the code is looping through the data and creating extra columns, not sure how to do this tho.?
And also the CreatePackingStats is located in a class and not in the page behind aspx.
Sorry about all the questions I am new and learning, all your help will help to develop and I appreciate all the help I receive.
Right-click on your GridView then go to the properties tab and select events.In there you will find the event called RowDataBound.
In that event write your code to change the forecolor like:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//here the Cells is an array where you can pass the index value of the cell where you want to check and if you don't know where the value is then you can do a for loop and then check the value
if (e.Row.Cells[0].Text == "someValue")
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
}
}
}
Update 1 for comparing the value using the IndexOf()
As for the data what you have given, you have to change the compare function from == to IndexOf("SomeValue").For that, you can try the IndexOf("actual"). If it gives value > -1 then change the color.
or you can try the below code where I am looping through all the columns in the row(you can try to avoid the looping if you have knowledge on which column the value will occur):
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.ToLower().IndexOf("actual") > -1)
{
e.Row.Cells[i].ForeColor = System.Drawing.Color.Red;
}
}
}
}
Update 2 Adding the snapshots of sample data and it's output.
Here is the sample data with which I am working:
And here is the processed output using the IndexOf() loop over the in RowDataBound event.
Hope this helps.
This may have to do with the page life cycle but just can't seem to get it working, even after finding every post out there on the subject.
In a gridview, a new header row is created with a control:
protected void gvNotes_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
CheckBox chk = new CheckBox();
chk.Text = "Show Admin Columns";
chk.ID = "chk";
chk.AutoPostBack = true;
chk.CheckedChanged += new EventHandler(this.chkShowAminColumns_CheckedChanged);
HeaderCell.Controls.Add(chk);
HeaderCell.ColumnSpan = gvNotes.Columns.Count;
HeaderGridRow.Cells.Add(HeaderCell);
gvNotes.Controls[0].Controls.AddAt(0, HeaderGridRow);
}
}
Trying to find this new control is giving me a little trouble:
public void bindNotesGrid()
{
DataTable dt = BLL.NotesBLL.GetNotes();
gvNotes.DataSource = dt;
gvNotes.DataBind();
if (dt.Rows.Count > 0)
{
//never finds the control
foreach (Control c in gvNotes.HeaderRow.Controls)
{
if (c is CheckBox)
{
string value = ((CheckBox)c).Text;
}
}
//never finds the control
//int current = 0;
//int headerCount = gvNotes.HeaderRow.Cells.Count;
//for (current = 0; current < headerCount; current++)
//{
// CheckBox chk2 = (CheckBox)gvNotes.HeaderRow.Cells[current].FindControl("chk");
//}
//returns null
CheckBox chk = (CheckBox)gvNotes.HeaderRow.FindControl("chk");
}
}
All the findcontrol attempts return the checkbox as null.
What am I not seeing here?
Thanks!
The header row is generated by the GridView control based on your data source. It is not actually a member of your dataset but is dynamically built based on the contents of the data set.
Consider this code:
int HeaderRowCount = -5; // initialized to a wrong starting point
protected void GridView1_DataBinding( object sender, EventArgs e ) {
HeaderRowCount = 0; // this event starts the binding and row creation process
}
// Each row is created and Bound in the order of the dataset.
protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e ) {
// Are we creating the Header Row?
if ( e.Row.RowType == DataControlRowType.Header ) {
// Create your additional Headers here: AddHeaderRow() is defined below
AddHeaderRow( ( GridView ) sender, "Hi I'm a header" );
}
}
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.Header ) {
HeaderRowCount++;
}
}
protected void GridView1_DataBound( object sender, EventArgs e ) {
GridView1.Caption = string.Format( "HeaderRowCount: {0}", HeaderRowCount);
}
After binding is complete you will see this in the GridView Caption:
HeaderRowCount: 1
The header row count will always be 1 because additional header rows are not part of the binding process. The GridView Rows collection only contain Rows that are part of the DataSet. GridViews generate the HeaderRow (and FooterRow) dynamically.
Any additional Header Rows are only available through the inner Table collection of rows, so you need to search each Table Row for RowType == Header:
Table InnerTable = ( Table ) GridView1.Controls[ 0 ];
foreach ( GridViewRow r in InnerTable.Rows ) {
if (r.RowType == DataControlRowType.Header){
CheckBox chk = (CheckBox) r.FindControl( "chk" );
}
}
Or you can access the rows directly if you wish:
GridViewRow r = ( GridViewRow ) InnerTable.Rows[0];
CheckBox chk = (CheckBox) r.FindControl( "chk" );
AddHeaderRow()
private void AddHeaderRow( GridView gv, string HeaderText ) {
Table InnerTable = ( Table ) gv.Controls[ 0 ];
GridViewRow row = new GridViewRow( 0, -1, DataControlRowType.Header, DataControlRowState.Normal );
TableCell th = new TableHeaderCell();
th.HorizontalAlign = HorizontalAlign.Center;
th.ColumnSpan = gv.Columns.Count;
th.Font.Bold = true;
th.Text = HeaderText;
row.Cells.Add( th );
InnerTable.Rows.AddAt( 0, row );
}
To find the control try this:-
foreach (Control c in gvNotes.Controls[0].Controls[0].Controls)
{
CheckBox chk = (CheckBox)c.FindControl("chk");
}
gvNotes.Controls[0].Controls -> System.Web.UI.WebControls.Table.RowControlCollection
gvNotes.Controls[0].Controls[0].Controls -> System.Web.UI.WebControls.TableRow.CellControlCollection
as first you added TableRow and then TableCell
Hop this will help
I would like to iterate through a datatable of rows using Next and Previous buttons.
My form has the following:
[Ticket Ref]
[Short Description]
[Next Step]
[Last Updated]
Through the life of a case, you might get multiple updates. Therefore I may have 5 rows in the database relating to a particular Ticket Reference. Comments and Last Updated will obviously be different.
When the form loads it will display the last record in the database into the text boxes i.e the last row from the query. I would then like to click Previous and see the previous rows. But if i click on Next I expect it to loop through. i.e go to the next record. So if im on record 1, then it needs to go to 2.
I have tried counting the clicks but this isnt very helpful as when the form first loads count will be 0 and if i click Previous then i will get a Row out of position -1 error.
My previous button looks like this:
protected void btnPrevious_Click1(object sender, EventArgs e)
{
DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
DataTable dt = new DataTable();
dt = eobj.GetTicketUpdates(txtSupportRef.Text);
int i = 0;
if (i < dt.Rows[0].Table.Rows.Count - 1 || i != 0)
{
i--;
txtShortDesc.Text = dt.Rows[0].Table.Rows[i]["ShortDesc"].ToString();
txtNextStep.Text = dt.Rows[0].Table.Rows[i]["NextStep"].ToString();
txtLastUpdated.Text = dt.Rows[0].Table.Rows[i]["LastUpdated"].ToString();
}
else
{
//no records to see more.
}
}
My next button looks like this:
protected void btnNext_Click1(object sender, EventArgs e)
{
DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
DataTable dt = new DataTable();
dt = eobj.GetTicketUpdates(txtSupportRef.Text);
int i = 0;
if (i < dt.Rows[0].Table.Rows.Count - 1)
{
i++;
txtShortDesc.Text = dt.Rows[0].Table.Rows[i]["ShortDesc"].ToString();
txtNextStep.Text = dt.Rows[0].Table.Rows[i]["NextStep"].ToString();
txtLastUpdated.Text = dt.Rows[0].Table.Rows[i]["LastUpdated"].ToString();
}
else
{
//no records to see more.
}
}
Also the user will have the ability to load different ticket references into the form. So i need the ability to quickly iterate based on the loaded ticket.
If you need more info please ask.
Your solution will work if you properly manage the index wrap-around.
You just need to reset the index to 0 when it hits the end of the data set, and set it to DataTable.Rows.Count - 1 when it goes back from the beginning.
INITIALIZE
DataTable dt;
protected override void Page_Load
{
// querying each load gets expensive; consider alternative patterns?
dt = GetData(txtSupportRef.Text);
if (!IsPostBack)
{
int i = 0;
ViewState["recordIndex"] = i;
PopulateForm(i);
}
}
CREATE REUSABLE FUNCTIONS - THEY SAVE YOU MILLION$
protected DataTable GetData(string supportRef)
{
DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
return eobj.GetTicketUpdates(supportRef);
}
protected void PopulateForm(int i)
{
ViewState["recordIndex"] = i
System.Data.DataRow row = dt.Rows[0].Table.Rows[i];
txtShortDesc.Text = row["ShortDesc"].ToString();
txtNextStep.Text = row["NextStep"].ToString();
txtLastUpdated.Text = row["LastUpdated"].ToString();
}
PREVIOUS BUTTON
protected void btnPrevious_Click1(object sender, EventArgs e)
{
int i = (int)ViewState["recordIndex"];
i = i <= 0 ? dt.Rows[0].Table.Rows.Count - 1 : i-1;
PopulateForm(i);
}
NEXT BUTTON
protected void btnNext_Click1(object sender, EventArgs e)
{
int i = (int)ViewState["recordIndex"];
i = i >= dt.Rows[0].Table.Rows.Count - 1 ? 0 : i+1;
PopulateForm(i);
}
Here is a JavaScript example that illustrates the algorithm and shows how much more responsive it is when doing this client side instead. It is applicable to C# also
var startWith = 1; // or 0, etc.
var pageNo = startWith;
var recordCount = 5;
var records = [];
for(var i = 0; i < recordCount; i++)
{
records.push({TicketRef: "000-00-"+ i, ShortDescription: ['BLUE SCREEN','VIRUS INFECTION','NEW HARDWARE','SOFTWARE UPGRADE','SPILLED SODA ON KEYBOARD','BROKEN CD-ROM'][Math.random()*6|0], NextStep: ['ESCALATE','RESOLVE','RETURN','CLOSE','INVESTIGATE','TRANSFER'][Math.random()*6|0], LastUpdated: ((Math.random()*12|0)+1).toString() + " hours ago" });
}
renderPageNo = function() {
$("h1").text(pageNo);
$("input:text").each(function(){this.value=records[pageNo-startWith][$(this).data("field")];});
}
prev = function() {
if (pageNo > startWith) pageNo--
else pageNo = recordCount;
renderPageNo(pageNo);
}
next = function() {
if (pageNo < recordCount) pageNo++
else pageNo = startWith;
renderPageNo(pageNo);
}
renderPageNo();
$(".btn").eq(0).on("click", prev).end().eq(1).on("click", next);
*
{
font-family: 'Segoe UI';
}
label
{
width: 200px;
display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1></h1>
<label>Ticket Ref<input type='text' data-field="TicketRef"/></label>
<label>Short Description<input type='text' data-field="ShortDescription"/></label>
<label>Next Step<input type='text' data-field="NextStep"/></label>
<label>LastUpdated<input type='text' data-field="LastUpdated"/></label>
<input class='btn' type='button' value='PREV' />
<input class='btn' type='button' value='NEXT' />
I would provide an index in your database table, which you can then load into the datatable together with the rest of the data. Then you can use that index to go forward and backwards with no hustle by extracting row details based on the index:
// store your current row index in a ViewState when first loading the data
ViewState["currentIndex"] = (int)row["RowIndex"];
//When going forward increment your rowIndex to find out the new Row Index
int currentRowIndex = (int) ViewState["currentIndex"];
currentRowIndex++; // ++ to go next -- to go previous
//Get the current row based on rowindex
DataRow row = myDataTable.Select("ID=" + currentRowIndex);
// then use the row data and assign to your textboxes
So for example a unique identity key, which is auto incremented by 1 in your database table will help to solve the problem.
Let me break it down a bit further:
Lets assume you fetch your database data on the page Load event:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
// Code to Fetch database data and assign it to a datatable
// Save DataTable in a ViewState["currentTable"]
// Get Last Row and assign field values to textboxes
// Get Last Row index value and assign it to ViewState["currentRowIndex"]
}
}
protected void cmdMoveNext_Click(){
// in this function you going to get your current row index from the ViewState[currentRowIndex]
int currentIndex = (int)ViewState["currentRowIndex"];
currentIndex++; // set next row index (currentIndex=currentIndex+1)
// declare a datatable and assing viewstate["currentTable"] to it
DataTable myTable = (DataTable)ViewState["currentTable"];
// Find the DataRow at the new index
DataRow row = (DataRow) myTable.Select("search by index code");
// Use the new row values to assign to textboxes
// Save the new row index in ViewState
}
I have done the same thing but much simpler and without using additional indexers.
Here is what I have done:
public OnLoad(...)
{
if(!Page.IsPostBack)
{
BuidNavigationList();
}
LoadNavigation();
}
private List<string> NavigationList
{
get
{
if (ViewState["NavigationList"] != null)
{
return (List<string>)ViewState["NavigationList"];
}
return null;
}
set
{
ViewState["NavigationList"] = value;
}
}
private void BuidNavigationList()
{
// ds get
if (ds.Tables.Count == 1)
{
DataTable dataTable = ds.Tables[0];
if (dataTable.Columns.Contains("YourSwitchColumn"))
{
NavigationList = new List<string>(dataTable.Rows.Cast<DataRow>().Select(r => r["YourSwitchColumn"]).Cast<String>());
}
}
}
private string YourCurrentSwitchColumnValue
{
get
{
return Request["YourSwitchColumn"].ToString();
}
}
private void LoadNavigation()
{
if(this.NavigationList == null)
{
lblPrevious.Enabled = false;
lblNext.Enabled = false;
}
int navigationListIndex = NavigationList.IndexOf(YourCurrentSwitchColumnValue);
if (navigationListIndex == 0)
{
lblPrevious.Enabled = false;
lblNext.Enabled = true;
lblNext.Value = NavigationList[navigationListIndex + 1];
}
else if (navigationListIndex > 0 && navigationListIndex < NavigationList.Count - 1)
{
lblPrevious.Enabled = true;
lblNext.Enabled = true;
lblPrevious.Value = NavigationList[NavigationListIndex - 1];
lblNext.Value = NavigationList[NavigationListIndex + 1];
}
else if (navigationListIndex == NavigationList.Count - 1)
{
lblPrevious.Enabled = true;
lblNext.Enabled = false;
lblPrevious.Value = NavigationList[NavigationListIndex - 1];
}
}
public void lblNext_OnClick(object sender, ...)
{
Request.Redirect("SamePage.aspx?YourSwitchColumn=" + valueFrom_lblNext);
}
Note this is a demo code you should implement on your needs
Solution, for at least a specific cell: GridView1.Rows[i].Cells[j].Text;
I've build a simple CSV-Fileupload. After the user uploaded the file he should be able to evaluate the data. When the fileupload was successful the data gets loaded into the GridView1, with this code: (Problem below the code)
string[] readCSV = File.ReadAllLines(lblFilePath.Text);
DataTable dt = new DataTable();
bool bSplitMe = false;
foreach (var rLine in readCSV)
{
if (bSplitMe)
{
string[] aSplittedLine = rLine.Split(";".ToCharArray());
try
{
dt.Rows.Add(aSplittedLine);
}
catch(System.Exception)
{
txtBoxFileOut.Text = rLine;
break;
}
}
else
{
if (rLine.ToLower().StartsWith("definedtestid;"))
{
bSplitMe = true;
string[] aSplittedLine = rLine.Split(";".ToCharArray());
foreach (var rCol in aSplittedLine)
{
dt.Columns.Add(rCol);
}
}
else
{
txtBoxFileOut.Text += rLine.ToString() + "\n";
}
}
}
dt.Columns.Remove("Column1");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "0";
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
After this the user should be able to select a row and display the data from that row in a chart.
Problem: I'm not able to read data from the cells I want, or to read from a "hardcoded" cell.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
GridViewRow row = GridView1.SelectedRow;
txtOutputfield.Text = row.Cells[2].Text;
}
Please check your cell index. Is it correct? For example: the third column will have index "2" not "3"
And, if you use a control to store the data, you need to find that control:
txtOutputfield.Text =
row.Cells[2].FindControl('placeyourcontrolnamehere').Text;
For a specific Cell this worked fine
txtOutputfield.Text = GridView1.Rows[i].Cells[j].Text;
The code below lets me show emails received in a listview on when the selected index is changed displays the body of the selected email in a RTB. The problem is i changed the code to work with a data grid view and now the Tag part wont work
void SomeFunc() // This line added by Jon
{
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
ListViewItem itmp = new ListViewItem(email.From);
ListViewItem.ListViewSubItem itms1 =
new ListViewItem.ListViewSubItem(itmp, email.Subject);
ListViewItem.ListViewSubItem itms2 =
new ListViewItem.ListViewSubItem(itmp, email.FromName);
itmp.SubItems.Add(itms1);
itmp.SubItems.Add(itms2);
listView1.Items.Add(itmp).Tag = i;
richTextBox1.Text = email.Body;
}
// Save the email to an XML file
bundle.SaveXml("email.xml");
}
private void listView1_SelectionChanged(object sender, EventArgs e)
{
if (listView1.SelectedCells.Count > 0)
{
// bundle is now accessible in your event handler:
richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedCells[0].Tag).Body;
}
}
Code for data grid view
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
string[] row = new string[] { email.From, email.Subject, email.FromName };
object[] rows = new object[] { row };
foreach (string[] rowArray in rows)
{
dataGridView1.Rows.Add(rowArray);
}
} // This line added by Jon
i have created earlier the code for datagrid view but you already done it so i haven't posted in your last question but i think , you should give a try to the below code.
// i am creating a new object here but , you can have a single object on the form
DataGridView dgv = new DataGridView();
private DataTable EmailSource { get; set; }
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.SelectionChanged+=new EventHandler(dgv_SelectionChanged);
Chilkat.MessageSet msgSet = imap.Search("ALL", true);
if (msgSet != null)
{
bundle = imap.FetchBundle(msgSet);
CreateDataTable();
if (bundle != null && dt!=null)
{
Chilkat.Email email;
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
if(email!=null)
{
DataRow drow = EmailSource.NewRow();
drow["Id"] = i.ToString();
drow["From"] = email.FromName;
drow["Subject"] = email.Subject;
drow["DateRecived"] = email.DateRecived;
// i am adding email body also
drow["Body"] =email.Body;
EmailSource.Rows.Add(drow);
}
}
// Save the email to an XML file
bundle.SaveXml("email.xml");
dgv.DataSource= EmailSource;
// Hiding Body from the grid
dgv.Columns["Body"].Visible =false;
}
}
// this event handler will show the last selected email.
void dgv_SelectionChanged(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rows = dgv.SelectedRows;
if (rows != null)
{
// get the last selected row
DataRow drow = rows[rows.Count - 1].DataBoundItem as DataRow;
if (drow != null)
{
richTextBox1.Text = drow["Body"];
}
}
}
private void CreateDataTable()
{
EmailSource = new DataTable();
EmailSource.Columns.Add("Id");
EmailSource.Columns.Add("From");
EmailSource.Columns.Add("Subject");
EmailSource.Columns.Add("DateRecived");
EmailSource.Columns.Add("Body");
}
You are adding rows using listView1.Rows.Add(rowArray) in both the code listings. Is that a typo or you named the GridView like that.
Basically, you are storing the index of the email in the "Tag" property.
listView1.Items.Add(itmp).Tag = i;
You need to make sure that you do the same while adding items to the GridView too.
The DataGridView does not have an "Items" collection. To make it work, you need to bind the DataGridView to a collection of objects. Something like this should get you started:
List<Email> emails = new List<Email>();
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
emails.Add(email);
}
dataGridView.ItemsSource = emails;
You should not need to store the row index for each item in a "Tag" object - you can can get the selected index like this:
int selectedIndex = dataGridView.SelectedCells[0].RowIndex;