I have this manually update code for my radgrid (RAD13).
on upload it works but the thing is that only the first row in the grid saves it's update.
I think that I should passe a value that will autoincrement to passe through rows
protected void UpdateButton_Click(object sender, EventArgs e)
{
RadGrid grid = (this.FindControl("RAD13") as RadGrid);
(grid.MasterTableView.GetItems(GridItemType.EditItem)[0] as GridEditableItem).FireCommandEvent(RadGrid.UpdateCommandName, string.Empty);
}
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
OnUpdateCommand="RadGrid1_UpdateCommand" AllowFilteringByColumn="true" AllowPaging="true"
AllowMultiRowEdit="true">
<MasterTableView DataKeyNames="ID" EditMode="InPlace">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="update All edit row" />
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name = "Name1"},
new { ID = 2, Name = "Name2"},
new { ID = 3, Name = "Name3"},
new { ID = 4, Name = "Name4"},
new { ID = 5, Name = "Name5"}
};
RadGrid1.DataSource = data;
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem item = e.Item as GridEditableItem;
UpdateLogic(item);
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridEditableItem item in RadGrid1.EditItems)
{
UpdateLogic(item);
item.Edit = false;
}
RadGrid1.Rebind();
}
protected void UpdateLogic(GridEditableItem item)
{
// perform your update logic here
}
Let me know if any concern.
Related
I have a gridview as shown below and i want to hide a column named FREQ_BAND
,second column in button click named btnPmtCalculation which is out side gridview .How to do this ? i reached till this code but not able to proceed
<asp:gridview id="gvSpectrum" runat="server" headerstyle-cssclass="HomeGridHeader"
allowpaging="True" rowstyle-wrap="false" autogeneratecolumns="False" cssclass="table table-striped table-bordered"
cellpadding="4" datakeynames="FEES_CALC_FORMULA,BAND_ID,BAND,FREQ_BAND,SPECTRUM_ID" horizontalalign="Left" showfooter="true"
pagesize="10" onpageindexchanging="gvSpectrum_PageIndexChanging" onrowcommand="gvSpectrum_RowCommand">
<Columns>
<asp:BoundField DataField = "sl_num" HeaderText="SN" />
<asp:BoundField DataField = "FREQ_BAND" HeaderText="Frequency Band" />
<asp:BoundField DataField = "BW" HeaderText="Bandwidth / Quantity" />
<asp:BoundField DataField = "RANGE" HeaderText="Range" />
</Columns>
</asp:gridview>
<asp:button id="btnPmtCalculation" runat="server" text="Generate Permenant Sepctrum Invoice" onclick="btnPmtCalculation_Click">
Code I did as shown below
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
bool pemenant = true;
int spectrum_id = 0;
//Button btn = (Button)sender;
////Get the row that contains this button
//GridViewRow gvr = (GridViewRow)btn.NamingContainer;
foreach (GridViewRow row in gvSpectrum.Rows)
{
if (row.RowType == DataControlRowType.Header)
{
row.Cells[1].Visible = true;
}
}}
You can try this:
OnRowCreated
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[columnIndex].Visible = false;
}
If you don't prefer the hard-coded index, the only workaround I can suggest is to provide a HeaderText for the GridViewColumn and then find the column using that HeaderText.
protected void UsersGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
((DataControlField)UsersGrid.Columns
.Cast<DataControlField>()
.Where(fld => fld.HeaderText == "Email")
.SingleOrDefault()).Visible = false;
}
OnButtonClick
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[Index].Visible = false;
}
Simple, just hide the column in the button click.
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[i].Visible = false;
}
you can change your code with this:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
bool pemenant = true;
int spectrum_id = 0;
int columnIndex = 1;
gvSpectrum.Columns[columnIndex].Visible = false;
}
'columnIndex' The column number is for hide it
or if you want hide with column Header Text Use this code:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
foreach(DataControlField col in gvSpectrum.Columns)
{
if (col.HeaderText == "Email")
col.Visible = false;
}
}
Try this:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[columnindex].Visible = false;
//OR
gvSpectrum.Columns["columnname"].Visible = false;
}
From code I add a TextBox to a column
protected void grdPartsBeingMonitored_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
foreach (GridColumn column in item.OwnerTableView.RenderColumns)
{
if (column.UniqueName == "MyColumn")
{
TextBox tbEditBox = new TextBox();
tbEditBox.Text = item[column].Text;
tbEditBox.Width = Unit.Pixel(50);
tbEditBox.ID = "editDemand";
item[column].Controls.Clear();
item[column].Controls.Add(tbEditBox);
}
}
}
Now how do I loop through each row and retrieve that rows value of the TextBox? Here's a start I believe:
foreach (GridDataItem item in grd1.MasterTableView.Items)
{
foreach (GridColumn column in item.OwnerTableView.RenderColumns)
{
if (column.UniqueName == "MyColumn")
{
//HOW TO RETRIEVE VALUE OF THE TEXTBOX HERE???
I tried this with no luck
foreach (Object c in item[column].Controls)
{
if (c.GetType() == typeof(TextBox))
{
TextBox tbEditBox = (TextBox)c;
System.Diagnostics.Debug.Write(tbEditBox.Text);
}
}
Please try with the below code snippet.
ASPX
<div>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView>
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID"></telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="Name" DataField="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Show edit" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Get edit value" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="hide edit" OnClick="Button3_Click" />
</div>
ASPX.CS
public bool IsEditable
{
get
{
if (ViewState["IsEdit"] == null)
return false;
else
return (bool)ViewState["IsEdit"];
}
set
{
ViewState["IsEdit"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "Name1");
dt.Rows.Add(2, "Name2");
dt.Rows.Add(3, "Name3");
RadGrid1.DataSource = dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
IsEditable = true;
RadGrid1.Rebind();
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
Response.Write((item.FindControl("txtName") as TextBox).Text);
//perform your DB update here
}
}
protected void Button3_Click(object sender, EventArgs e)
{
IsEditable = false;
RadGrid1.Rebind();
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
Label lblName = item.FindControl("lblName") as Label;
TextBox txtName = item.FindControl("txtName") as TextBox;
lblName.Visible = !IsEditable;
txtName.Visible = IsEditable;
}
}
Let me know if any concern.
I have below code to create a gridview in asp.net and inside gridview I have a delete button. Below code works fine and shows Delete in all rows.
I want to hide/ Disable the Delete button in very first row. Can somebody suggest the code part?
<asp:gridview ID="Gridview1" runat="server"
ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Cat">
<ItemTemplate>
<asp:TextBox ID="TextBoxCat" runat="server" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" >
<ItemTemplate>
<asp:LinkButton ID="DeleteItemsGridRowButton" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
You can use GridView.RowDataBound Event event.
Then find the LinkButton using FindControl method.
public class Animal
{
public int RowNumber { get; set; }
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gridview1.DataSource = new List<Animal>
{
new Animal {RowNumber = 1, Name = "One"},
new Animal {RowNumber = 2, Name = "Two"},
new Animal {RowNumber = 3, Name = "Three"},
new Animal {RowNumber = 4, Name = "Four"},
};
Gridview1.DataBind();
}
}
private int _counter;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (_counter == 0)
{
var linkButton = e.Row.FindControl("DeleteItemsGridRowButton")
as LinkButton;
linkButton.Visible = false;
_counter++;
}
}
}
Try This:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(GridView1.Rows.Count>0)
{
//GridView1.Rows[0].Visible = false;
LinkButton DeleteItemsGridRowButton= (LinkButton) GridView1.Rows[0].FindControl("DeleteItemsGridRowButton");
if(DeleteItemsGridRowButton!=null)
{
DeleteItemsGridRowButton.Visible=false
}
}
}
i'm a newby at .NET and I'm having a problem with my checkboxes. They all return false, even if they are selected. Here is my asp code
<asp:GridView ID="gvGeneros1" runat="server" class="divTable"
AutoGenerateColumns="False" DataKeyNames="idgenero" CssClass="table">
<Columns>
<asp:BoundField DataField="nome" HeaderText="GĂȘnero" SortExpression="nome" >
<ControlStyle Width="200px" />
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:TemplateField AccessibleHeaderText="Check">
<ItemTemplate>
<asp:CheckBox ID="checkGenero" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and here's my c# code
DataSet dsDivided;
protected void Page_Load(object sender, EventArgs e)
{
Music musicbll = new Music();
DataSet dsGeneros = musicbll.getGenders();
int size = dsGeneros.Tables[0].Rows.Count;
dsDivided = null;
// Divide in two DataTable
dsDivided = Tools.SplitDataTableInTwo((DataTable)dsGeneros.Tables[0], size / 2);
gvGeneros1.DataSource = dsDivided.Tables["FirstSet"];
gvGeneros1.DataBind();
for (int i = 0; i < gvGeneros1.Rows.Count; i++)
{
((CheckBox)gvGeneros1.Rows[i].Cells[1].Controls[1]).Checked=false;
}
}
protected void btGravarPrefs_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable("generos");
dt.Columns.Add("idgenero", typeof(int));
dt.Columns.Add("active", typeof(bool));
for (int i = 0; i < gvGeneros1.Rows.Count; i++)
{
int idCliente = (int)dsDivided.Tables[0].Rows[i][0];
bool check = ((CheckBox)gvGeneros1.Rows[i].Cells[1].Controls[1]).Checked; //always false
dt.Rows.Add(new object[] { idCliente, check});
}
}
}
I don't know what to try more, and i search all over and it seems right. Thanks
I think you should wrap your Data binding with if (!Page.IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Bind your datasource here
}
}
I use a GridView to show data from an excel file, after setting the GridView's Allowpage to true. All the pages show the same content as that in the first page. Thanks to anyone who tell me why!
The HTML code of the GrdiView is:
<asp:GridView ID="GridView_NewStaff" runat="server" AutoGenerateColumns="False"
CellPadding="4" Font-Size="Small" ForeColor="#333333" AllowPaging="True"
AllowSorting="True" onpageindexchanging="GridView_NewStaff_PageIndexChanging"
PageSize="20">
<pagersettings firstpagetext="First" lastpagetext="Last">
Mode="NextPreviousFirstLast" NextPageText="Next" PageButtonCount="4"
PreviousPageText="Previous" />
<columns>......
The C# code is:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGridView();
}
}
protected void Button_ReadFile_Click(object sender, EventArgs e)
{
bindGridView();
}
public void bindGridView()
{
if (FileUpload_StaffInfo.HasFile)
{
string InfoFile_Name = Server.MapPath(InfoFileDirectory) + FileUpload_StaffInfo.FileName;
FileUpload_StaffInfo.SaveAs(InfoFile_Name);
dataTable_Excel = new DataTable();
if (InfoFile_Name.EndsWith(".xlsx"))//Read Excel2007-2010 format file
{
StaffExcelXML StaffNewXML = new StaffExcelXML();
StaffNewXML.ExcelInput(InfoFile_Name, ref dataTable_Excel)
}
else if (InfoFile_Name.EndsWith(".xls"))//Read Excel1997-2003 format file
{
StaffXLS StaffNewXLS = new StaffXLS();
StaffNewXLS.ExcelInput(InfoFile_Name, ref dataTable_Excel);
}
GridView_NewStaff.DataSource = dataTable_Excel;
GridView_NewStaff.DataBind();
}
}
protected void GridView_NewStaff_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_NewStaff.PageIndex = e.NewPageIndex;
bindGridView();
}
change
GridView_NewStaff.PageIndex = e.NewPageIndex;
bindGridView();
to
GridView_NewStaff.PageIndex = e.NewPageIndex;
GridView_NewStaff.DataBind();
After assigning a new Page index to the GridView, just rebind the GridView control.