In my asp.net application, I have used Gridview control, In which i have to add Dropdownlist at runtime for each cell.Which i am able to bind successfully.
Below is my code which inside row databound event,
foreach (GridViewRow row in gdvLocation.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
for (int i = 1; i < row.Cells.Count; i++) {
var dlRouteType = new DropDownList();
dlRouteType.ID = "ddlRouteType";
dlRouteType.DataSource = GetRouteTypeList();
dlRouteType.DataTextField = "RouteType";
dlRouteType.DataValueField = "Id";
dlRouteType.DataBind();
row.Cells[i].Controls.Add(dlRouteType);
}
}
}
I have a button in my page, which has functionality to save data to database . While saving data i have to pass the value from Dropdownlist which i have added at runtime. On button click i am writing following code to get data from dropdownlist,
var ddlDropDown = (DropDownList)row.Cells[i].FindControl("ddlRouteType");
But i am getting null in ddlDropDown object. I have even added Update panel inside aspx page. Any suggessions most welcome.
Thanks in advance
Sangeetha
You have these errors in your code
RowDataBound already iterates through each rows and so all you need not write that foreach on top
You are iterating from index 1, index are zero-based. So start from zero.
The DropDownList ID must be unique, so better write something like,dlRouteType.ID = "ddlRouteType_" + i;
The code should be,
protected void gdvLocation_RowDataBound(object sender, GridViewRowEventArgs e)
{
//removed the foreach loop
var row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < row.Cells.Count; i++) //changed index
{
var dlRouteType = new DropDownList();
dlRouteType.ID = "ddlRouteType_" + i; //gave unique id
dlRouteType.DataSource = GetRouteTypeList();
dlRouteType.DataTextField = "RouteType";
dlRouteType.DataValueField = "Id";
dlRouteType.DataBind();
row.Cells[i].Controls.Add(dlRouteType);
}
}
}
Related
Currently I try to use Repeater WebControl in order to display a table that list out all possible component. Below is my table;
Right now I try to merged cells in Group Code and Group Description column that have the same value. Below is my merging cell code, noted that the code is in class;
public void repeaterRowSpan(string repeaterID, string columnID)
{
var pageHandler = HttpContext.Current.CurrentHandler;
Control ctrl = ((Page)pageHandler).Master.FindControl("ContentPlaceHolder3").FindControl(repeaterID);
Repeater repeaterName = (Repeater)ctrl;
for (int i = repeaterName.Items.Count - 1; i > 0; i--)
{
HtmlTableCell oCell_previous = (HtmlTableCell)repeaterName.Items[i - 1].FindControl(columnID);
HtmlTableCell oCell = (HtmlTableCell)repeaterName.Items[i].FindControl(columnID);
oCell.RowSpan = (oCell.RowSpan == -1) ? 1 : oCell.RowSpan;
oCell_previous.RowSpan = (oCell_previous.RowSpan == -1) ? 1 : oCell_previous.RowSpan;
if (oCell.InnerText == oCell_previous.InnerText)
{
oCell.InnerText = "";
oCell_previous.RowSpan += oCell.RowSpan;
}
}
}
Somehow the code manage to delete the same value in the column but maintain the rowspan. When I debugged, the oCell_previous.RowSpan return '2' so the code itself working fine. Below is the result of merging;
How can I modified my code in such way it will merged the cell?
In your opinion, between Repeater and GridView which is most suitable to show data in table form in this project? In my understanding, Repeater is most suitable since it faster than GridView. GridView is only suitable if you have edit function to go with your table.
This can be implemented using the OnDataBound event. The OnDataBound event of the GridView is executed after the GridView is populated with the records. Executed a loop in reverse over the GridView Rows and then the common Cells are identified and merged into single cell.
Below sample code merges the first & second columns (assuming that there are redundant values by comparing the above row(s)),
Feel free to leave a comment if you need more info.
protected void OnDataBound(object sender, EventArgs e)
{
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
for (int j = 0; j < row.Cells.Count; j++)
{
if (row.Cells[j].Text == previousRow.Cells[j].Text)
{
if (previousRow.Cells[j].RowSpan == 0)
{
if (row.Cells[j].RowSpan == 0)
{
previousRow.Cells[j].RowSpan += 2;
}
else
{
previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
}
row.Cells[j].Visible = false;
}
}
}
}
}
I have a Data grid view in which I have changed all the cells to text boxes to allow for a bulk update. The problem is that the users new input values are not registering and instead the cells are holding the old values. I taught it might be to do with a post back issue but cant imagine it is as there is no loading of old data in the page load. I also considered that the text boxes might not be registering properly because they are added without an ID but not to sure on that.
Changed cells to textboxes here.
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 4; i < row.Cells.Count; i++)
{
TextBox test = new TextBox();
test.Text = row.Cells[i].Text.ToString();
row.Cells[i].Controls.Add(test);
}
}
trying to pull new values and them to list newValues here
foreach(GridViewRow row in GridView1.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
bool isChecked = row.Cells[0].Controls.OfType < CheckBox > ().FirstOrDefault().Checked;
if (isChecked) {
string table = DropDownList1.Text.ToString();
List < string > selectedValues = CheckBoxList1.Items.Cast < ListItem > ()
.Where(li = > li.Selected)
.Select(li = > li.Value)
.ToList();
List < string > newValues = new List < string > ();
int userid = Convert.ToInt32(row.Cells[1].Text.ToString());
//GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label) row.FindControl("WDB_Id");
//TextBox txtname=(TextBox)gr.cell[].control[];
for (int i = 4; i < selectedValues.Count + 4; i++) {
newValues.Add(row.Cells[i].Text.ToString());
}
}
}
}
Thanks
I have a loaded DataGrid. In that DataGrid the first column is a CheckBox, and the second column is "Name". Also I have saved one field "Name" in the database . Here I want to make the CheckBox to be checked if the Name is equal to the data which I stored in the database.
Here my problem is that ,I am getting only one CheckBox to to be checked.
ex:[ If my expected result is 1st, 2nd and 3dr CheckBoxes to be checked , But I am getting only the 3rd one as checked. ]
my sample code is
foreach (GridViewRow row in GrdProduct.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lblproduct = (Label)row.FindControl("lblProduct");
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelectAll");
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
DataRow r = dt.Rows[rowIndex];
if (Convert.ToString(r["productName"]) == lblproduct.Text)
{
chkSelect.Checked = true;
}
else
{
chkSelect.Checked = false;
}
}
}
Finally I got the solution ... Simply removed the else part
else
{
chkSelect.Checked = false; }
--
thank you all...
Sorry I am blind. You are looping through the datarows and of course you most likely have only one match. In all other cases it becomes unchecked right after. Use the following:
foreach (GridViewRow row in GrdProduct.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lblproduct = (Label)row.FindControl("lblProduct");
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelectAll");
chkSelect.Checked = false;
for (int rowIndex = 0; rowIndex < dt.Rows.Count || !chkSelect.Checked; rowIndex++)
{
DataRow r = dt.Rows[rowIndex];
if (Convert.ToString(r["productName"]) == lblproduct.Text)
{
chkSelect.Checked = true;
}
}
}
Als when comparing strings I recommend http://msdn.microsoft.com/en-us/library/system.string.equals(v=vs.110).aspx
COmpared what you are going to do with the data and controls in the grid I also recommend additional validation.
Can you please help me on this, I get the check box control value as false always even when I check the control in gridview.
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox CheckBox1 = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox1 != null)
{
if (CheckBox1.Checked)
{
query = GridView1.Rows[i].FindControl("Label1") + ",";
}
}
}
Are you databinding on Page_Load method? If yes, you must do this:
if(!IsPostBack)
{
GridView1.DataSource = YourData;
}
If you don't do this, your DataGridView will be databound even if it is a PostBack. This way no matter what you checked, the DataGridView will be repopulated from the data source for your postbacks.
Using the above code, when you do if(!IsPostBack), it will retain the checkbox's viewstate value and you get the correct Checked status.
Inside the checkbox design add the following attribute
ToolTip="<%#Container.DataItemIndex+1 %> " and then following is the code behind
for (int i = 0; i < gdview.Rows.Count; i++)
{
string labeldetail = "";
CheckBox cbox = (CheckBox)gdview.Rows[i].Cells[0].FindControl("CheckBox1");
if (cbox != null)
{
if (cbox.Checked == true)
{
int rowsNo = (Convert.ToInt16(city.ToolTip) - 1); //Convert.ToInt16(SrNo);
labeldetail = ((Label)gdview.Rows[rowsNo].FindControl("labelid")).Value;
}
I have a ListBox with number of items matching the number of rows in the GridView. Using this ListBox I want to display a tooltip for each row only to the first column data in the GridView. I have bound the GridView in the front end.
The code I have tried is giving Index of out range error :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i <= GridView1.Rows.Count; i++)
{
String ProCol = GridView1.Rows[i].Cells[0].ToString();
if (ProCol.Length != 0)
{
e.Row.Cells[0].ToolTip = ListBox1.Items[i].ToString().Trim();
}
}
}
}
It fails in the last iteration of your for cycle, since the index is zero based (first item has 0 index, last item has count-1 index). Replace
i <= GridView1.Rows.Count
with
i < GridView1.Rows.Count
Try moving your code to the DataBound event on the GridView instead of doing the work in RowDataBound. That way, it will only run once when all the rows have been bound and the GridView.Rows collection has been initialized.
Also, you should do what Michal Klouda wrote in his answer regarding <= an <.
And you must make sure that the ListBox1 has been databound before you bind the GridView.
Sample code that should work as long as you bind the ListBox befor the GridView:
protected void GridView1_DataBound(object sender, EventArgs e)
{
var gv = (GridView)sender;
for (int i = 0; i < gv.Rows.Count; i++)
{
var oneRow = gv.Rows[i];
String ProCol = oneRow.Cells[0].ToString();
if (ProCol.Length != 0)
{
oneRow.Cells[0].ToolTip = ListBox1.Items[i].ToString().Trim();
}
}
}