Edit all rows for one column in a GridView - asp.net

I would like to be able to bulk edit all the rows for one column using a GridView control. What is the best way to do this?

If you want to update all rows with same value then show proper control(textbox/dropdown/checkbox/radio) in column header
else
show the grid column in edit mode instead of label.
See following:
http://www.codeproject.com/KB/webforms/BulkEditGridView.aspx

I guess u know this: http://msdn.microsoft.com/en-us/library/ms972948.aspx

Probably not the best, but an option is to set the primary key of your table as the DataKey of the GridView then iterate the grid and use the datakey and the edited value to update the DB. Here is an example.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID">
<Columns>.....
foreach (var item in GridView1.Items)
{
var id = (Guid)GridView1.DataKeys[item.DataItemIndex].Value;
var txt= item.FindControl("AmountTextBox") as Textbox;
if (cb != null && id.HasValue)
UpdateRow(id.Value, txt.Text);
}

Related

show gridview headers when binding the gridview with a list

I would like to show headers of a gridview which contains no data:
List<myData> datas = new List<myData>();
Gridview1.DataSource = datas.ToArray();
Gridview1.DataBind();
But then headers will be not shown. How can I show the headers in case the datas is empty?
I am still using .NET Framework 2.0, the server does not support .NET 4.0
Thanks in advance.
Check this one:
Show Grid view header and footer when the grid view is empty (with Generic List)
from your code I think the .ToArray() call is not needed, a GridView should be able to bind also to the List directly.
Use the GridView EmptyDataTemplate to display the headers when there's no data.
example:
<asp:GridView>
<emptydatatemplate>
<table><tr><td>Header 1</td><td>Header 2</td><td>Header 3</td><td>Header 4</td></tr></table>
</emptydatatemplate>
</asp:GridView>
you could try something like this...
//Check to see if we get rows back, if we do just bind.
if (grdview.Rows.Count != 0)
{
grdview.DataSource = dtFunding;
grdview.DataBind();
}
else
{
//Other wise add a emtpy "New Row" to the datatable and then hide it after binding.
grdview.Rows.Add(grdview.NewRow());
grdview.DataSource = dtFunding;
grdview.DataBind();
grdview.Rows[0].Visible = false;
}

How to put values in textbox from listbox in jQuery MVC ASP.NET

I have one Listbox named "List1" and one button says "Append".
I have on textbox named "TextDescription".
I want to put the select values from the listbox to textbox on click of append button.
So can anyone tell me how to do this?
You could use the .val() function. So assuming you have a select with id="myselect" and a text input with id="mytext" you could do this:
var values = $('#myselect').val();
if (values != null) {
// concatenate the selected values with , so that we can add them to the textbox
$('#mytext').val(values.join(','));
}
And here's a live demo illustrating it in action.

Get the value of hidden column in Gridview

I am using a Gridview and wondering if there is way to hide a column from the Gridview but still be able to access the hidden column value.
I set visible= false for the column that I want to hide but when I try to get the value of the column, the value in the column is empty.
Thanks.
One way I have gotten values from Invisible GridView Columns is using the DataKeyNames attribute.
<asp:GridView runat="server" ID="GridView" DataKeyNames="ColName1, ColName2">
</asp:GridView>
then to access the data
var data = GridView.DataKeys[RowIndex].Values[KeyIndex]
If you're in the RowDataBound event, get the row DataItem:
if(e.Row.RowType == DataControlRowType.DataRow)
{
var dataRowView = (DataRowView)e.Row.DataItem;
var data = dataRowView["FieldName"].ToString();
}

Show header of a empty Gridview for Datasource is List<Data>

I am using a Gridview with datasource is a List. How can I show the header if the List is null or for a empty gridview?
if you are using fx4.0 then Set ShowHeaderWhenEmpty to true in Grid view
please look at below code
asp:GridView ID="gvPreview" runat="server" ShowHeaderWhenEmpty="True"
See A more elegant solution to display GridView header and footer when the data source is empty.
You can use HeaderTemplate property to setup the head programatically or use ListView instead if you are using .NET 3.5.
or
You can even try below
//if data exists
if (dtSource.Rows.Count != 0)
{
grdView.DataSource = dtSource;
grdView.DataBind();
}
else
{
//Other wise add a emtpy "New Row" to the datatable and then hide it after binding.
dtFunding.Rows.Add(dtSource.NewRow());
grdView.DataSource = dtSource;
grdView.DataBind();
grdView.Rows[0].Visible = false;
}

issue in gridview[checkbox and images]

' />
'/>
this is my code where i am trying to display checkbox and images
my table has columns
itemID Imagespath
1 item1.jpg
2 item2.jpg
3 item3.jpg
4 item4.jpg
but in output it is showing as i want to show only checkbox and image
but now it is showing checkbox along with the [ID i.e 1 ,2,3 etc ] which i should make as [ID as invisiable in design but in code behind i should be able to get the selected value ID]
is there ant way i can remove the gridview border and row border.
actually i am trying to amke a checkboxlist alaong with the images, right now even images are not getting displayed
thank you
You can leverage the GridView's DataKeys property for this:
<asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID">
In your code you can loop through the rows and read the data key for that row and get the checkbox like this:
foreach (GridViewRow gvr in gvImages.Rows)
{
int CustomerID = (int)gvImages.DataKeys[gvr.RowIndex].Value;
CheckBox CheckBox1 = (CheckBox)gvr.FindControl("CheckBox1");
}
Text property of Checkboxes not used for server side value. Checkboxes don't have a value property. They have only Checked property for value.
In your case you should add an hidden field in your row that will hold the value of ID column, and you can get it at server side.

Resources