how to set row number in grid view - c#

I need to set number of row in data grid view (like identity in sql) in winforms. i am dynamically adding columns and rows in grid view. any idea.

To display text in the row header you can use the Row.HeaderCell.Value as shown below:
void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
DataGridView gridView = sender as DataGridView;
if (null != gridView)
{
foreach (DataGridViewRow r in gridView.Rows)
{
gridView.Rows[r.Index].HeaderCell.Value =
(r.Index + 1).ToString();
}
}
}
This only displays the row number of the new row when the user begins typing in it. Not sure if there is a simple way of always showing the row number on the new row.
Another best way for asp.net application.Here is a link http://www.devcurry.com/2010/01/add-row-number-to-gridview.html
Just add the following tags to your section of your GridView
<Columns>
<asp:TemplateField HeaderText="RowNumber">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>

Method 1:
//You can also use below code
this.DataGridView1.Rows[e.RowIndex].Cell[0].value =e.RowIndex +1;
//get total number of rows
this.DataGridView1.Rows.Count;
Method 2:
for (int i = 0; i < SensorGridView.Rows.Count; i++)
{
DataGridViewRowHeaderCell cell = SensorGridView.Rows[i].HeaderCell;
cell.Value = (i + 1).ToString();
SensorGridView.Rows[i].HeaderCell = cell;
}
Method 3:
void GridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
this.GridView.Rows[e.RowIndex].Cells[0].Value
= (e.RowIndex + 1).ToString();
}

Why not like this way. Build a datatable first and then bind to grid like below
DataTable tbl = new DataTable();
Datacolumn dc = new Datacolumn("ID");
Datacolumn dc1 = new Datacolumn("col1");
Datarow dr = tbl.NewRow();
dr[dc] = "123";
dr[dc1] = "SomeData";
tbl.Rows.Add(dr);
DataGridView.DataSource = tbl;

Related

Avoid postback when getting dropdownlist value c# asp

I've dynamically created a dropdownlist within C# (No code in asp):
DropDownList myDDL = new DropDownList();
This can't be called from pageLoad() as it's not loaded everytime the page refreshes. However it's loaded everytime OnPostback(OnSelectedIndexChanged()) of another dll so I cant do !IsPostBack.
This dll is created when:
A value of another dropdownlist populates a GridView using OnSelectedIndexChanged()
<asp:DropDownList id ="select1" name="assignGetForm" runat="server" class="selectClass" AutoPostBack="True" OnSelectedIndexChanged="populateGridView">
On the GridView a function is run OnRowDatabound()(when the above OnIndexChanged() populates it) to populate myDDL inside the GridView
<asp:GridView id="GridView1" name="GridView1" onrowdatabound="populateCellsDDL" runat="server"></asp:GridView>
I'm now trying to access the values in myDDL using a button onclick() event - however this always does postback and refreshes the page so myDDL dissapears and when I print the value to console it just gives my first value select activity and not the one i've actually selected.
How would I get these values when they are changed as this dll is not populated on pageLoad().
I've tried looking at AJAX however i'm unsure how to look at the c# value through this. I tried aswell viewstates but i dont have luck as again it doesnt populate on pageLoad().
I think, you need to rebind the grid in each postback, if you want to access values of dynamically created controls.
I tried to simulate the situation with below sample code and I was able to access the value successfully.
ASPX code
<asp:DropDownList ID="select1" name="assignGetForm" runat="server" class="selectClass" AutoPostBack="True" />
<asp:GridView ID="GridView1" name="GridView1" OnRowDataBound="GridView1_DataBound" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
C# code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
FillMainDll();
if (!IsPostBack || GridView1.Rows.Count > 0)
BindGrid();
}
private void FillMainDll()
{
select1.DataSource = new int[] { 1, 2, 3 };
select1.DataBind();
}
private void BindGrid()
{
var dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
for (int i = 1; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = "Name - " + i.ToString();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var myDDL = new DropDownList();
myDDL.ID = "myDDL";
myDDL.DataSource = GetGridRowDdlData();
myDDL.DataBind();
e.Row.Cells[1].Controls.Add(myDDL);
}
}
private IEnumerable<string> GetGridRowDdlData()
{
var data = new List<string>();
for (int i = 1; i < 4; i++)
{
data.Add("Name - " + i * int.Parse(select1.SelectedValue));
}
return data;
}
protected void Button1_Click(object sender, EventArgs e)
{
var sb = new System.Text.StringBuilder();
foreach (GridViewRow row in GridView1.Rows)
{
var myDDL = row.FindControl("myDDL") as DropDownList;
if (myDDL != null)
{
sb.AppendFormat("{0}<br/>", myDDL.SelectedValue);
}
}
Response.Write(sb.ToString());
}

How to get values from multiple DropDownLists in a GridView on button_click?

I have a GridView on my asp.net webform with all the columns and rows created in C# code from behind. Except the first column, every cell in each column is a DropDownList with a DataSource.
Here is my GridView:
<asp:GridView ID="gv_Rota" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_Rota_RowDataBound">
<HeaderStyle BackColor="#6a3d98" ForeColor="White" Height="20" />
<RowStyle HorizontalAlign="Center" Height="20px" Width="100px" />
<AlternatingRowStyle Height="20px" />
</asp:GridView>
And the c# creation of the columns and cells:
private void BindGrid(int Amount)
{
gv_Rota.DataSource = null;
gv_Rota.Columns.Clear();
BoundField bfield = new BoundField();
bfield.HeaderText = "Days";
bfield.DataField = "Days";
gv_Rota.Columns.Add(bfield);
for (int i = 0; i < Amount; i++)
{
int week = i + 1;
string sWeek = "Week " + week.ToString();
TemplateField tfield = new TemplateField();
tfield.HeaderText = sWeek;
gv_Rota.Columns.Add(tfield);
}
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Days", typeof(string)));
dt.Rows.Add("M");
dt.Rows.Add("T");
dt.Rows.Add("W");
dt.Rows.Add("T");
dt.Rows.Add("F");
dt.Rows.Add("S");
dt.Rows.Add("S");
gv_Rota.DataSource = dt;
gv_Rota.DataBind();
}
And then on the RowDataBound trigger of my GridView, I add the DropDownList control to every cell:
protected void gv_Rota_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 1; i <= ColumnCount; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddlShift = new DropDownList();
ddlShift.ID = "ddlShift";
ddlShift.DataSource = DCListOfShifts;
ddlShift.DataValueField = "SHIFT_ID";
ddlShift.DataTextField = "SHIFT_NAME";
ddlShift.DataBind();
ddlShift.Items.Insert(0, new ListItem("Shift..."));
ddlShift.CssClass = "ddl_rotamanager";
e.Row.Cells[i].Controls.Add(ddlShift);
}
}
}
What is displayed for example on my page:
So my question and problem is that I now want to save each column (except the first) to a database, as a "scheduled week", and have no idea how to get the values of selected items in all or any of the DropDownLists to then pass that back to the database. Baring in mind, this is NOT done on selectedindexchanged. I will select items from all the DropDownLists and then press a button to submit the values. Please could someone give me some highlight on how this could be done? Thanks.
EDIT 1: Response explains how I could get every value of a DropDownList in a row or by individual cell. However, now I need to know more specifically how I can get values of specific cells in the columns rather than the rows. Is it possible to get further highlight on this?
Use Gridview.Rows property
protected void MyButton_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in gv_Rota.Rows)
{
//find this control in this row
DropDownList dd = row.FindControl("MyDropdown") as DropDownList;
//OR
//find this control in a specific cell
DropDownList day = row.Cells[0].FindControl("MyDropdown") as DropDownList;
DropDownList Week1 = row.Cells[1].FindControl("ddShift") as DropDownList;
//this is just a pseudo-code to determine which dropdown was selected per row.
//You can improve on this
if(dd1.SelectedValue != string.Empty)
thisDay.SelectedWeek = dd1.SelectedValue;
else if(dd2.SelectedValue != string.Empty)
thisDay.SelectedWeek = dd2.SelectedValue;
....
}
}

Textbox not showing up in GridView

I am manually creating a datatable to apply it to a gridview and the textbox column is not showing up. The other columns work just fine but I need to create a column with inputtable text boxes.
Cheers,
Chris
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int numPrizes = GetNumPrizes();//if there are voided prizes.
DataTable dt = new DataTable();
gvTournamentData.Columns.Clear();
//PrizeNumber
//Add the columns to the grid view
BoundField PrizeNumberBF = new BoundField();
PrizeNumberBF.DataField = "PrizeNumber";
PrizeNumberBF.SortExpression = "PrizeNumber";
PrizeNumberBF.HeaderText = "";
gvTournamentData.Columns.Add(PrizeNumberBF);
dt.Columns.Add("PrizeNumber");
//Place
//Add the columns to the grid view
BoundField PlaceBF = new BoundField();
PlaceBF.DataField = "Place";
PlaceBF.SortExpression = "Place";
PlaceBF.HeaderText = "Place";
gvTournamentData.Columns.Add(PlaceBF);
dt.Columns.Add("Place", typeof(String));
DataTable dtPlaces = GetPlaces(1043); //hard coded for now
////PrizeName //BFOP Prize
//Add the columns to the grid view
BoundField PrizeNameBF = new BoundField();
PrizeNameBF.DataField = "BFOP Prize";
PrizeNameBF.SortExpression = "BFOP Prize";
PrizeNameBF.HeaderText = "BFOP Prize";
gvTournamentData.Columns.Add(PrizeNameBF);
dt.Columns.Add("BFOP Prize", typeof(String));
DataTable dtPrizeNames = GetPrizeNames(1043); //hard coded for now
////NickName
dt.Columns.Add("NickName", typeof(TextBox));
numPrizes = 7; //hard coded for now
for (int index = 1; index <= numPrizes; index++)
{
// Start at Prize one instead of Prize zero
string ID = index.ToString();
DataRow row = dt.NewRow();
row["PrizeNumber"] = ID + ")";
row["Place"] = dtPlaces.Rows[index - 1]["Place"];
row["BFOP Prize"] = dtPrizeNames.Rows[index - 1]["PrizeName"];
//TextBox nicknameTB = new TextBox();
//nicknameTB.Text = ID + ")";
//row["NickName"] = nicknameTB;
/* for (int x = 0; x < numCols; x++)
{
PopulatePrizeNameLabel(ID);
PopulateAddButton(ID);
}*/
dt.Rows.Add(row);
}
gvTournamentData.DataSource = dt;
gvTournamentData.DataBind();
gvTournamentData.Visible = true;
}
}
Why not use TemplateColumn in your form page
Example:
<asp:GridView>
<Columns>
<asp:TemplateColumn HeaderText="Place">
<ItemTemplate>
<%#Eval("Place")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="NickName">
<ItemTemplate>
<asp:TextBox ID="txt_NickName" runat="server" Width="85px" Text='<%#Eval("NickName")%>'>
</asp:TextBox>
</ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:GridView>
So you lay out your template you would want to bind to. The approach above is more flexible.
So when you come down to binding your data to the grid view:
gvTournamentData.DataSource = dt;
gvTournamentData.DataBind();
Then it will render all those fields to a template.
Use this link as a reference: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview%28v=vs.110%29.aspx

Postback resets the selectedValue of DropdownList inside GridView

My gridview has 2 columns - DropDownList & TextBox. The DDL is databound by a datatable. The SelectedIndex Change of DDL will populate its Textbox and also add a new row. All of this works fine, but the selected values of DDLs of previous rows, reset to 0 index, when a new row is added.The textbox values remain intact though. How can I retain the DDL selected values when adding new rows.
For eg. if I select 'abc' from dropdown of row 1, it populates its text field and also adds a new row. But with the postback happening, abc does not remain selected in row 1 ddl:
LOGIN PHONE
1 123456789
2
ASPX:
<asp:GridView ID="gvCommissions" runat="server"
OnRowDataBound="gvCommissions_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="LOGIN" ItemStyle-Width="29%">
<ItemTemplate>
<asp:DropDownList ID="ddlLogin" runat="server" Width="98%"
DataTextField="login" DataValueField="id"
OnSelectedIndexChanged="ddlLogin_SelectedIndexChanged" AutoPostBack="true" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PHONE" ItemStyle-Width="12%">
<ItemTemplate>
<asp:TextBox ID="txtPhone" runat="server" Width="98%"
Text='<%# Eval("phone")%>' Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Get values from DB, store to a datatable and then to a viewstate
GetAgentDetails();
//Adds an empty row to grid
SetInitialRow();
}
}
private void AddNewRow()
{
//Create a datatable
dtCurrentData = new DataTable();
dtCurrentData.Columns.Add("phone");
//Store values of each row to a new datarow. Add all rows to datatable
foreach (GridViewRow gvRow in gvCommissions.Rows)
{
DataRow drcurrentrow = dtCurrentData.NewRow();
drcurrentrow["phone"] = ((TextBox)gvRow.FindControl("txtphone")).Text;
dtCurrentData.Rows.Add(drcurrentrow);
}
//create an empty datarow and also add it to the new datatable.
DataRow dr = dtCurrentData.NewRow();
dr["phone"] = "";
dtCurrentData.Rows.Add(dr);
//Bind the new datatable to the grid
gvCommissions.DataSource = dtCurrentData;
gvCommissions.DataBind();
}
protected void gvCommissions_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlLogin");
dtAgents = (DataTable)ViewState["AgentsTable"];
ddl.DataSource = dtAgents;
ddl.DataBind();
ddl.Items.Insert(0, "");
}
}
When you databind in AddNewRow, the original rows are lost. You must store the dropdown selected index, together with your textbox phone value in dtCurrentData. You can then set the index of the dropdowns in your RowDataBound event, using the values your saved in dtCurrentData.
private void AddNewRow()
{
//Create a datatable
dtCurrentData = new DataTable();
dtCurrentData.Columns.Add("phone");
//new column for dropdown index
dtCurrentData.Columns.Add("ddlIndex");
//Store values of each row to a new datarow. Add all rows to datatable
foreach (GridViewRow gvRow in gvCommissions.Rows)
{
DataRow drcurrentrow = dtCurrentData.NewRow();
drcurrentrow["phone"] = ((TextBox)gvRow.FindControl("txtphone")).Text;
//get dropdown index
drcurrentrow["ddlIndex"] = ((DropDownList)gvRow.FindControl("ddlLogin")).SelectedIndex;
dtCurrentData.Rows.Add(drcurrentrow);
}
//create an empty datarow and also add it to the new datatable.
DataRow dr = dtCurrentData.NewRow();
dr["phone"] = "";
//initial drop down index
dr["ddlIndex"] = 0;
dtCurrentData.Rows.Add(dr);
//save to the viewstate like your AgentsTable
ViewState["CurrentData"] = dtCurrentData;
//Bind the new datatable to the grid
gvCommissions.DataSource = dtCurrentData;
gvCommissions.DataBind();
}
protected void gvCommissions_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlLogin");
dtAgents = (DataTable)ViewState["AgentsTable"];
ddl.DataSource = dtAgents;
ddl.DataBind();
ddl.Items.Insert(0, "");
//get the dropdown index from CurrentData
//use the current gridview's row index, since it matches the datatable
if (ViewState["CurrentData"] != null)
{
DataTable dtCurrentData = (DataTable)ViewState["CurrentData"];
ddl.SelectedIndex = Convert.ToInt32(dtCurrentData.Rows[e.Row.RowIndex]["ddlIndex"]);
}
}
}
RowDataBound gets fired every time (irrespective of whether the page is PostBack page or NOT).
You need to add that if(!IsPostBack) condition around the code where you are performing the databinding for the dropdowns in your RowDataBound event.

Retrieving all GridViewRow objects from a GridView control with paging enabled

I currently have a GridView control on my aspx page with paging enabled and I need to loop through the entire row collection/count to process the selected records. With my current code, it will only loop through the current page of GridView row.
What is the best way to accomplish this task?
Here is my current code:
ASPX page:
<asp:GridView ID="MyGridView" runat="server" AllowPaging="true" PageSize="20">
<Columns>
<!-- My Column list -->
</Columns>
</asp:GridView>
<asp:Button id="MyButton" runat="server" Text="Add" OnClick="MyButton_Click" />
code behind:
protected void MyButton_Click(object sender, EventArgs e)
{
for (int Count = 0; Count < MyGridView.Rows.Count; Count++)
{
//The row count is 20 and only contains the GridViewRow object in the current page view
//I want to retrieve the all GridViews rows so I can add them to a ReorderList control
}
}
Yes because your gridview UI is only aware of the current page.
Get the datasource and determine the row count from there...
int count = ((DataTable)MyGridView.DataSource).Rows.Count;
//or
int count = ((ICollection<SomeRecord>)MyGridView.DataSource).Count;
Simply use the following code:
//Change gridview to
GridView1.AllowPaging = false;
GridView1.DataBind();
//Transfer rows from GridView to table
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < GridView1.Rows[0].Cells.Count; j++)
{
//Add your code here..
}
}
}
//After filling your datatable change gridview paging style back to first, ie.
GridView1.AllowPaging = true;
GridView1.DataBind();
This may help you, let me know if this was helpful for you...
I think you should get the row count from your data source's row count.
If you need to filter rows, you can use DataTable's / DataView's Select method.
EDIT : You can not get actual row count by gridview.Rows.Count if gridview is paged. Depending on your comment, I assume that you're using listDataSource generic list to bind your gridview, you can get your row count as :
List<DataSourceItem> selectedRows =
listDataSource.FindAll(delegate(DataSourceItem item)
{
// Assuming you have a IsSelected bool property
// that refers your row is selected :
return item.IsSelected;
});
int rowCount = selectedRows.Count;
use session or state to store:
protected void Set_CheckboxStatus()
{
CheckBox selectall = (CheckBox)EmployeeGrid.HeaderRow.FindControl("gcb_selectall");
ArrayList cbstatuslist = new ArrayList();
if (Session["childcbstatus"] != null)
{
cbstatuslist = (ArrayList)Session["childcbstatus"];
}
foreach (GridViewRow row in EmployeeGrid.Rows)
{
int cb_index = (int)row.DataItemIndex; //For Getting DataItemIndex of EmployeeGrid
//int cb_index = (int)row.RowIndex;
CheckBox cb_selemp = (CheckBox)row.FindControl("gcb_selemp");
CheckBox cb_active = (CheckBox)row.FindControl("gcb_active");
if (cb_selemp.Checked == true)
{
if (!cbstatuslist.Contains(cb_index))
cbstatuslist.Add(cb_index);
}
else
{
cbstatuslist.Remove(cb_index);
}
}
Session["childcbstatus"] = cbstatuslist;
}
from the arraylist you can get all row index to loop and get the value from the gridview with paging.
#CRice's answer should have been the official answer.
Here is my solution. You need to presave the gridview's data, via its DataSource, into ViewState or Session.
GridView.Rows only refers to the "visible" rows, or the page currently shown on the screen.
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
DataSourceSelectArguments dss = new DataSourceSelectArguments();
//get the datasource related to the gridview
string wsDataSourceID = (gv.DataSourceID == string.Empty) ? ViewState["DataSourceID"].ToString() : gv.DataSourceID;
SqlDataSource sds = (SqlDataSource)pnlMAIN.FindControl(wsDataSourceID);
if (sds != null)
{
//load the data again but this time into a dataview object
DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty);
if (dv != null)
{
//convert the dataview to a datatable so we can see the row(s)
DataTable dt = (DataTable)dv.ToTable();
if (dt != null)
{
//Save your data before changing pages
ViewState["AllTheData"] = dt;
gv.DataSource = dt;
gv.DataSourceID = null;
}
}
}
//now change pages!
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
Next, when changing pages, here we save the data
protected void GridView_PageIndexChanged(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
DataSourceSelectArguments dss = new DataSourceSelectArguments();
//reload the datatable back to the gridview
gv.DataSource = ViewState["AllTheData"];
gv.DataSourceID = null;
gv.DataBind();
I hope the code speaks for itself.
Thanks

Categories