I have a grid-view on which each row consists check box, and there is a button outside the grid-view. when i check some rows on grid-view and clicking those button, i need to insert the checked rows details into another table of database. I have created the grid-view like this
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="student_name" HeaderText="student_name" SortExpression="student_name" />
<asp:BoundField DataField="student_id" HeaderText="student_id" SortExpression="student_id" ReadOnly="True" />
<asp:BoundField DataField="student_nric" HeaderText="student_nric" SortExpression="student_nric" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox AutoPostBack="false" Id="CheckBoxUpdate" runat="server" />
</ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
but dont know how to store the checked rows value to datatable. please help
On button click on which you want to save the values in the database.
Loop through earch row of the gridview.
Find the checkbox in that row
Check if it is checked or not
foreach (GridViewRow row in GridView1.Rows)
{
if (((CheckBox)row.FindControl("CheckBoxUpdate")).Checked)
{
//insert here
}
}
Here is a good link for the same
http://www.c-sharpcorner.com/Forums/Thread/201835/loop-through-gridview.aspx
You can use a stringbuilder object to iterate through the rows of the checkbox and store the value. see this tutorial
http://www.codeproject.com/Articles/11207/Selecting-multiple-checkboxes-inside-a-GridView-co
Related
I have below GridView in .aspx file.
<asp:GridView ID="grdScopeList" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ScopeChk" runat="server" Checked=false />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sync_scope_name" Visible=true />
</Columns>
</asp:GridView>
I am using Dataset/DataTable for assiging the GridView.Below is the Code snippet used in PageLoad event.
string scopecomm="select sync_scope_name from Sync.scope_info";
DataSet ds_scope = new DataSet();
ds_scope = GetData(scopecomm, remoteconn); grdScopeList.DataSource = ds_scope;
grdScopeList.DataBind();
But I am getting the column sync_scope_name twice.I am able to bind the column sync_scope_name in Dataset to BoundField.I just need to show it once.
Regards,
Sachin K
Most likely your query is returning duplicates.
Run your query manually against your database to discard this.
If the query should return duplicates and you only want to show them once, you need to group your query
select sync_scope_name
from Sync.scope_info
group by sync_scope_name
Or use the distinct keyword
select distinct sync_scope_name from Sync.scope_info
I added AutoGenerateColumns="false" in GridView.
I have a grid like below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" s
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:ButtonField DataTextField="Name" HeaderText="Name" />
<asp:BoundField DataField="ArrDate" DataFormatString="{0:MM/dd/yyyy}"
HeaderText="Arr Date" />
<asp:BoundField HeaderText="Dep Date" DataField="DepDate"
DataFormatString="{0:MM/dd/yyyy}" />
<asp:BoundField HeaderText="Mail" DataField="Mail" />
<asp:BoundField HeaderText="Status" DataField="Status" />
<asp:BoundField DataField="ResId" HeaderText="ResId" Visible="False" />
</Columns>
</asp:GridView>
In Code Behind:-
try
{
string text = GridView1.Rows[2].Cells[5].Text;
ScriptManager.RegisterStartupScript(this, GetType(), "Message", "alert('ResId = " + text + ".');", true);
}
catch { }
Now the message shows - RegId =.
I can't get the value. So I change the RedId BoundField as vissible. Now I got the Value.
that is RegId =6.
I have two issue now -
1) How to get the RegId value in Non Visible Column.
2) How I find the Row value which i click... bzs the only i can change the ROWVALUE in code..
string text = GridView1.Rows[ROWVALUE].Cells[5].Text;
That is certainly not the right way to do it. You might want to look at using DataKeys to achieve this. With current approach, when you add a new column to your grid your code will fail.
Add your RegId column inside DataKeys property on your gridview
Reference your gridview datakey for the current row in codebehind like this
int regId= Convert.ToInt32(YourGridview.DataKeys[rowIndex]["RegId"].ToString());
I have two fields in my gridview. One is a field with a date range and one of them is a buttonfield. I want to make it so that when a button is clicked, it will redirect them to a specific page with a formatted string similar to how it's done when a hyperlinkfield is clicked as such:
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}"
However, I don't want to use the other field's value(the date range). I want to use the primary key of the record which is "SundayOfWeek". The user wants a button rather than a hyperlink. Is there a way I can do the same thing with a button?
Try using this type of column instead of a ButtonField:
<Columns>
<asp:HyperLinkField
DataTextField="ProductName"
HeaderText="Product Name"
SortExpression="ProductName"
DataNavigateUrlFields="ProductID"
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}" />
</Columns>
Sure, one of many ways is this:
<columns>
<asp:templatefield headertext="Button">
<itemtemplate>
<asp:Button id="btnRedirect"
Text= "Click me"
CommandArgument='<%#Eval("SundayOfWeek")%>'
OnClick="DoRedirect"
runat="server"/>
</itemtemplate>
</asp:templatefield>
</columns>
protected void DoRedirect(object sender, EventArgs e)
{
Button theButton = sender as Button;
Response.Redirect("Newpage.aspx?ID="+theButton.CommandArgument);
}
If you don't want to use the date-range field, you should use another column as DataNavigateUrlField:
DataNavigateUrlField="KeyField"
DataTextField="DateField"
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}"
If you don't want to use a HyperLinkColumn, you should use a TemplateField and add a Button:
<asp:TemplateColumn>
<input type="button" onclick="location.href='ProductInfo.aspx?ProductID=<%#Eval("KeyField")%>'" value='<%#Eval("DateField")%>'' />
</asp:TemplateColumn>
I'm trying to use CheckBoxes within a GridView's TemplateField to select multiple entries from that GridView. The GridView's data source is a list of items that is generated at page load.
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False"
AllowPaging="True" onpageindexchanging="TANsGridView_PageIndexChanging"
DataKeyNames="GUID">
<Columns>
<asp:TemplateField ShowHeader="False" HeaderText="Checker">
<ItemTemplate>
<asp:CheckBox ID="SelectCheckbox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
The problem is that when I press a submit button, all the CheckBoxes are returned with the Checked property as 'false'.
For cycling through the rows, I use:
foreach (GridViewRow row in TANsGridView.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("SelectCheckbox");
}
What should I use in order to have access to the correct value?
Thanks, Catalin
Are you mistakingly rebinding the gridview on page load every time? The gridview binding code should be wrapped in a if statement ensuring it's only done on not postback.
Am I supposed to put this here for an accept check now? :)
if your are binding the grid on page load.load the grid like this.
if(!ispostback)
{
..........loading data to databind.
}
Here's how I want the control to work. Click edit on a row in gridview. There's a textbox in one of the columns, I would like it to be blank even though there's data in the database for that column, but when I click update I would like whatever new text the user entered to update the database for that column. It would be one-way databinding, but the other way?
Here's how I did it using an sql datasource with the select,update and delete methods generated.
First, you'll need to make any column that you want to edit like this a template field with an item template and and edit item template:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="City" SortExpression="City">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCityEdit" Text=""></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Next handle the gridview's on update event:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//find the value control who's value you want
TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCityEdit");
//since we are not using a bound column for city we need to explicitly insert the value
//into the update parameter.
SqlDataSource1.UpdateParameters["City"].DefaultValue = tb.Text;
}
Even if you are using something other than an SQL Datasource this should be the basic solution.
// Find notes textbox
TextBox tb = (TextBox)MyActiveReferrals.Rows[e.RowIndex].FindControl("NotesTextBox");
e.NewValues.Add("Notes", tb.Text);
I used a linq data source.