I have Gridview with 10 Rows each row have 2 columns which is Textbox and Checkbox.
what i have to do is have to enter textbox Value as 1000 and i Clicked the Checkbox at first Row then value must go to Textbox of Second row and i clicked checkbox of Second Row then value must be go to third row of Textbox and so on.
i tried this,
protected void txtintroId_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
GridViewRow grid = ((GridViewRow)txt.Parent.Parent.Parent);
TextBox txtIntro = (TextBox)txt.FindControl("txtintroId");
}
here i get the 1st row Value but How do i pass this to second Row.
Assist me
Here is full working code(as per your requirement )
Create a gridview like this
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("txtcolumn") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Add a function in code behind C#
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
int rowNo=0;
foreach (GridViewRow GV1 in GridView1.Rows)
{
rowNo++;//
if (rowNo < GridView1.Rows.Count) //Checking that is this last row or not
{
//if no
CheckBox chkbx= (CheckBox)GV1.FindControl("CheckBox1");
TextBox curenttxtbx = (TextBox)GV1.FindControl("TextBox1");
if (chkbx.Checked == true )
`{
int nextIndex = GV1.RowIndex + 1;//Next row
TextBox txtbx = (TextBox)GridView1.Rows[nextIndex].FindControl("TextBox1");
txtbx.Text = curenttxtbx.Text;
}
}
//if yes
else
{
//For last row
//There is no next row
}
}
}
And i used this data table as data source
DataTable table = new DataTable();
table.Columns.Add("txtcolumn", typeof(string));
table.Rows.Add("1");
table.Rows.Add("32");
table.Rows.Add("32");
table.Rows.Add("32");
table.Rows.Add("3");
table.Rows.Add("32");
table.Rows.Add("32");
I tested this code it is working as per your requirement. (And SORRY for this bad formatting. i will do it later or please some body correct the formating :))
Related
I have a GridView in a ASP.NET web application, in which I have added image button in each row:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="edit" runat="server" CommandArgument='<%# Bind("EmpID") %>' CommandName="edituser" ImageUrl="image/images.jpg"
ToolTip="Edit User Details"> </asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
Now how I can get the row data from gridview simply by clicking an edit image button in a row?
You have to change the CommandArgument with this one:
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
Then:
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "edituser") /*if you need this
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the buttonfrom the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here now you have the specific row data
}
}
Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label _lblText = (Label)this.GridView1.SelectedRow.FindControl("UR_Desired_Label_ID");
this.TextBox3.Text = _lblText.Text ;
}
I have used grid view in my ASP.NET application.
<asp:GridView ID="grdView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkbox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Jurisdiction">
<ItemTemplate>
<asp:Label ID="lblJurisdiction" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="License Number">
<ItemTemplate>
<asp:TextBox ID="txtLicenseNumber" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in cs file
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
for (int i = 0; i < grdView.Columns.Count; i++)
{
String cellText = row.Cells[i].Text;
}
}
}
Note that the above grid will be populated by data. Now I need to get data from already populated gridview. The above code is not working. Also I need to retrieve from labels, textboxes, checkboxes values inside grid. Please help !!!
You can use FindControl method to retrieve the control's data:-
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
CheckBox chkbox = row.FindControl("chkbox") as CheckBox;
Label lblJurisdiction = row.FindControl("lblJurisdiction") as Label;
..and so on
//Finally retrieve the data like your normal control
string labelText = lblJurisdiction.Text;
}
}
Use GridViewRow.FindControl method.
foreach (GridViewRow row in grdView.Rows)
{
// return you the check-box control from current row
var chkbox = row.FindControl("chkbox");
}
Check whether the row type is DataControlRowType.DataRow.
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < grdView.Columns.Count; i++)
{
String cellText = row.Cells[i].Text;
}
}
}
}
To get the TextBox, CheckBox value from the grid use this,
string TextBoxvalue = ((TextBox)GridViewID.Rows[i].FindControl("TextBoxName")).Text;
string CheckBoxvalue = ((CheckBox)GridViewID.Rows[i].FindControl("CheckBoxName")).Text;
In your code .cs file, you are missing to check only for datarow. As it will check all 3 places:-
1. Header
2. Body
3. Footer
Might any one place, any exception can occur.
Please add one more if condtion just after the for loop, as below.
if (row.RowType == DataControlRowType.DataRow)
{
Hope this post will help's you :).
Use
cells[i].EditedFormatedValue
Okay so I have a gridview as the following html shows:
<asp:GridView ID="gridDetaljiNarudzbe" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" runat="server" OnRowCommand="gridDetaljiNarudzbe_RowCommand" OnPageIndexChanging="gridDetaljiNarudzbe_PageIndexChanging" OnRowDataBound="gridDetaljiNarudzbe_RowDataBound">
<Columns>
<asp:BoundField DataField="Naziv" HeaderText="Naziv" />
<asp:BoundField DataField="Sifra" HeaderText="Šifra" />
<asp:BoundField DataField="Cijena" HeaderText="Cijena" />
<asp:BoundField DataField="Kolicina" HeaderText="Količina" />
<asp:TemplateField HeaderText="Ocjena">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnOcijeni" title="Ocijeni proizvod" CommandName="OcijeniCommand" CommandArgument='<%# Eval("ProizvodID") %>' runat="server"><img src="../images/ocijeni.png" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For filling out the dropdown list for each record in DB I have used the following code (RowDataBound event):
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList drop = e.Row.FindControl("DropDownList1") as DropDownList;
drop.Items.Add(new ListItem(""));
drop.Items.Add(new ListItem("1"));
drop.Items.Add(new ListItem("2"));
drop.Items.Add(new ListItem("3"));
drop.Items.Add(new ListItem("4"));
drop.Items.Add(new ListItem("5"));
}
For selecting a value from the dropdown list in each row:
foreach (GridViewRow gr in gridDetaljiNarudzbe.Rows)
{
DropDownList drop = gr.FindControl("DropDownList1") as DropDownList;
// now selecting a value from dropdownlist
int selectednumber = Convert.ToInt32(drop.Text);
}
Now my problem is, lets say we have two records in the grid, and I would like to pick up the value from the second dropdown list in 2nd row (lets suppose I did that). And when I press the button of the first record in first row, that value which I have picked up from 2nd dropdown list is now inserted into the DB as if I selected something from the first drop list.
Also I would like to know if its possible after that something has been picked up in drop list and inserted into the DB, to disable now that dropdownlist and turn into static text which would show just 5 instead of the dropdownlist?
Can someone help me out with this please? I've tried everything but I haven't succeeded yet. :/
EDIT:
When I select something from the second grid(2nd dropdownlist as u can see the picture) and when I press the button to grade the first product, I get the grade from the 2nd dropdown list which I have selected. Is it any more clear now?
Thanks!
Edit here is the code for yogi:
List<hsp_Proizvodi_SprijeciDvaPutaOcijeniti_Result> lista = ServisnaKlasa.SprijeciDvostrukoOcjenjivanje(ProizvodID, Sesija.kupac.KupacID);
foreach (GridViewRow gr in gridDetaljiNarudzbe.Rows)
{
if (lista.Count == 0)
{
DropDownList drop = gr.FindControl("DropDownList1") as DropDownList;
if (drop.SelectedIndex != 0)
{
Ocjene o = new Ocjene();
o.KupacID = Sesija.kupac.KupacID;
o.ProizvodID = ProizvodID;
o.Datum = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
o.Ocjena = Convert.ToInt32(drop.Text);
ServisnaKlasa.OcjenjivanjeProizvoda(o);
string poruka = "Proizvod uspješno ocijenjen!";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + poruka + "');", true);
drop.SelectedIndex = 0;
}
}
else
{
string poruka = "Ovaj proizvod ste već ocijenili!";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + poruka + "');", true);
}
}
Follow this approach
add gridview's RowCommand event.
Give a command argument for your link button.like this
<asp:LinkButton ID="btnOcijeni" title="Ocijeni proizvod" CommandName="OcijeniCommand" CommandArgument='<%#Eval("ProizvodID") + ";" +((GridViewRow) Container).RowIndex%>' runat="server"><img src="../images/ocijeni.png" /></asp:LinkButton>
(i just added one more field in command argument for rowindex)
now where ever click on the link button the RowCommand event will be called.
protected void gridDetaljiNarudzbe_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "OcijeniCommand")
{ arg = e.CommandArgument.ToString().Split(';');
int index = Convert.ToInt32(arg[1].ToString());
//Finding same row dropdown list
//This will give you same row dropdownlist not the below row
DropDownList drpdwn1= (DropDownList)gridDetaljiNarudzbe.Rows[index].FindControl("DropDownList1");
//Now for BoundField values
GridViewRow row = GridView1.Rows[index];
string NazivText= row.Cells[0].Text;//It will give Naziv's value
//same way you can get other field values
//And save into db.
}
}
Edit
For converting the drop down to label
Add a label below your drop down list and make it Visible=false
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Label ID="lbl_ForDrpdwnVal" runat="server" Visible="false"></asp:Label>
Now in the same item command event add the below code after inserting values to db.
//Access the new label
Label lblfordrpdwn= (Label)gridDetaljiNarudzbe.Rows[index].FindControl("lbl_ForDrpdwnVal");
lblfordrpdwn.Text= drpdwn1.SelectedItem.Text;//Setting value from dropdown to lbel
lblfordrpdwn.Visible=true;//Showing label
drpdwn1.Visible=false;//Hiding the dropdown
I am using GridView in asp.net like this:
mygrid.DataSource = dTable;
mygrid.DataBind();
if (mygrid.Columns.Count > 1)
{
mygrid.Columns[2].Visible = false;
}
my grid view code is as follows
<asp:GridView ID="mygrid" runat="server" AllowPaging="True"
onpageindexchanging="mygrid_PageIndexChanging" PageSize="15"
PersistedSelection="true"
ondatabound="mygrid_DataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("Value", "~/myweppage.aspx?Id=M{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings PageButtonCount="4" />
</asp:GridView>
Here I am not able to set visible=false.
I tried with the following answer
How do I make several gridview columns invisible dynamically?
I am not finding datarow event in Visual Studio 2010. Can anyone help me to set the column visible property?
my Column structure of data table is
column[0] is Value column then 4 other columns are there.
my Column structure of Grid view is
column[0] is link field
column1 is Value field from Dtable
4 other columns
This is perfect solution for dynamically generated columns in gridview
Please try this :
int indexOfColumn = 1; //Note : Index will start with 0 so set this value accordingly
protected void mygrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells.Count > indexOfColumn)
{
e.Row.Cells[indexOfColumn].Visible = false;
}
}
For .aspx page edit gridview tag as follow :
<asp:GridView ID="mygrid" runat="server" AllowPaging="True"
onpageindexchanging="mygrid_PageIndexChanging" PageSize="15"
PersistedSelection="true"
ondatabound="mygrid_DataBound"
OnRowDataBound="mygrid_RowDataBound">
Here is the simple answer. Create css as below
.classHide{ display:none }
then instead of column.visible = false, just assign classHide CSS class to the column.
e.g.
grdRole.Columns(0).ItemStyle.CssClass = "classHide"
grdRole.Columns(0).HeaderStyle.CssClass = "classHide"
*strong text*Try to make use of the event ItemDataBound event and try the following syntax to hide the column dynamically:
mygrid.Columns[1].Visible = false //(Example)
Column count for a datatable starts from 0 not from 1 . so if it is the second column , you want to hide, index should be 1.
Hope this helps..
right Click on gridview and select Properties then select Events you will find there RowDataBound Double Click on it and in Row data bound write this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
Try this:
for (int i = 0; i < YourGrid.Rows.Count; i++)
{
(YourGrid.Columns[2]).Visible = false;
}
I know there are a bunch of questions with detailed instructions on exporting from gridview to excel, but I can't find my particular situation.
I have a gridview that displays records with five fields from a search. Users can check in a checkbox any number of records. On a button click, I can successfully export only the checked records to Excel. Export is as HTML. I'm using the technique from Matt Berseth here: http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
I added a check for whether the record was checked by the user and this works fine.
But I have a requirement that for the checked records that are exported, the users want to see the entire records (i.e. all fields in just the selected records).
What is a good strategy to accomplish this?
I've tried retrieving all fields in the gridview and setting all but the five desired fields to not visible. Then in the export button click event, setting the fields to visible and rebinding. No luck there.
Thanks for any help.
Sounds like you should loop through the rows to see which are checked.
foreach (GridViewRow row in gridView.Rows)
{
if(row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = row.FindControl("CheckBoxID") as CheckBox;
if(cb != null && cb.Checked)
{
// Logic here.
}
}
From there, you can export to excel using several different mechanisms, depends on your needs. One I've used in the past if you just need xls files is NPOI -- it's open source and provides a decent framework for it. Here is a link to their site:
http://npoi.codeplex.com/
-- EDIT
After reading your comments, maybe this will help.
ASPX Code:
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Checkbox Column">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxID" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Visible Column">
<ItemTemplate>
<asp:Label ID="lblVisibleColumn" runat="server" Text='<%# Eval("VisibleColumn")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hidden Column" Visible="false">
<ItemTemplate>
<asp:Label ID="lblHiddenColumn" runat="server" Text='<%# Eval("HiddenColumn")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
Backend Code:
public class MyDataColumn
{
public string visibleColumn { get; set; }
public string hiddenColumn { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dataTable = new DataTable();
List<MyDataColumn> dataColumns = new List<MyDataColumn>();
for (int i = 0; i < 10; i++)
{
dataColumns.Add(new MyDataColumn()
{
visibleColumn = string.Format("Visible Column {0}", i),
hiddenColumn = string.Format("Hidden Column {0}", i)
});
}
gridView.DataSource = dataColumns;
gridView.DataBind();
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
List<MyDataColumn> dataColumnsToExport = new List<MyDataColumn>();
foreach (GridViewRow row in gridView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = row.FindControl("CheckBoxID") as CheckBox;
if (cb != null && cb.Checked)
{
Label lblVisibleColumn = row.FindControl("lblVisibleColumn") as Label;
Label lblHiddenColumn = row.FindControl("lblHiddenColumn") as Label;
dataColumnsToExport.Add(new MyDataColumn()
{
visibleColumn = lblVisibleColumn.Text,
hiddenColumn = lblHiddenColumn.Text
});
}
}
}
GridView gridViewToExport = new GridView();
gridViewToExport.DataSource = dataColumnsToExport;
gridViewToExport.DataBind();
//Do Something With gridViewToExport
//GridViewExportUtil.Export("GridView.xls", gridViewToExport);
}
And this should be easy to convert to a DataTable if needed -- I used my own class to make it shorter code.