Gridview created duplicate rows - c#

I have a Gridview that populate with an item from the listview is click.
However my Gridview will create duplicate rows upon trigger from list view item command.
My listview and gridview are on the same page.
Any solution to avoid that?
The following code is how i bind data to gridview from listview item_command method:
protected void loadDataEvent(string categoryid)
{
Module_Category mc = new Module_Category();
gvEventByCategory.DataSource = mc.Get_All_Event_By_Category(categoryid);
gvEventByCategory.DataBind();
}
protected void lkbtnCatItem_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Enter")
{
loadDataEvent(e.CommandArgument.ToString());
}
}

Related

How to get selected row index in devexpress gridcontrol?

I have devexpress gridcontrol which looks like that:
I have click event on this red X button:
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
}
How can get there row index where this button is ?
You cannot access rows on GridControl, since this is just a container for the views.
As I can see from your picture you're using GridView. When you press the delete button, focused row changes and you can access it via FocusedRowHandle.
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
var gv = myGridControl.MainView as GridView;
var index = gv.FocusedRowHandle;
gv.DeleteRow(index);
}
You can use the GridView.FocusedRowHandle property:
view.DeleteRow(view.FocusedRowHandle);

how to get the column index of the gridview

I have two image buttons in the GridView used for selection. They populate the textboxes. What I need is when i click imagebutton "Edit" it should populate the textboxes from the gridview and when the imagebutton "Delete" is pressed it does the same and disbale the textboxes too. The point is am not getting a logic of how to get the column indexes programmatically.
help me out with the condition.
the issue is just with the c# code. Here is the snippet which i have tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var row = GridView1.SelectedRow;
if (condition)
{
_PropertyTitle.Text = row.Cells[1].Text;
_Address.Text = row.Cells[2].Text;
}
else
{
_PropertyTitle.Text = row.Cells[1].Text;
_PropertyTitle.Enabled = false;
_Address.Text = row.Cells[2].Text;
_Address.Enabled = false;
}
}
}
It is not very clear from question that what you are trying to achieve.But what i got is you want to write specify both image button click separately.
for that you have to use the RowCommand event.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
In this row command event according to CommandName (of button) you can manage the code for two buttons
Refer this link for Rowcommand event row command
One more link

How Do I Bind Dropdown To Value Present In Database

I am using datalist control an asp.net using C# and sqlserver 2008.I have a dropdown in datalist and need to display it's current value from database on page load.
I have tried this so far,
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
DropDownList ddlshowit = (DropDownList)e.Item.FindControl("DropDownList4");
ddlshowit.DataSource = ds;
ddlshowit.DataTextField = "showit";
ddlshowit.DataValueField = "showit"; //showit is my column name
ddlshowit.DataBind();
}
}
I also tried adding the following declaration in markup: SelectedValue='<%#Eval("showit")%>' but it also didn't work. Please Help
You can find your dropdownlist from your datalist in this way,
Protected void Page_load(object sender,Eventargs e)
{
foreach(DataList dl in DataList1.Items)
{
DropDownList ddlshowit = (DropDownList)dl.FindControl("DropDownList4");
}
}
Let me know the output.

Dropdownlist values comes null on button click.Dropdownlist is placed at first row of gridview

Here is code that i used to access dropdownlist inside gridview on button click.I know its due to postback of page.But how to fix it.
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow row;
row = dgData.Rows[0];
DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));
}
On page load i have called the method to bind gridview.
if (page.ispostback==false)
{
grdbind();
}
try with 0th cell if it is first
(row.Cells[0].FindControl("ddlCol1"))

Dynamically adding Page size dropdown list in the Gridview Pager

I have a Gridview for which Dropdown list has to be added on the run time at the Pager row. I have added the below code on the Gridview RowCreated.
protected void gv_transaction_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
using (DropDownList ddlpagesize = new DropDownList())
{
ddlpagesize.Items.Add("25");
ddlpagesize.Items.Add("50");
ddlpagesize.Items.Add("75");
ddlpagesize.Items.Add("100");
ddlpagesize.Items.Add("150");
ddlpagesize.Items.Add("200");
ddlpagesize.AutoPostBack = true;
ddlpagesize.Items.FindByText(gv_transaction.PageSize.ToString()).Selected = true;
ddlpagesize.SelectedIndexChanged += ddlpagesize_SelectedIndexChanged;
using (Table tbl = (Table)e.Row.Cells[0].Controls[0])
{
using (TableCell cell = new TableCell())
{
cell.Controls.Add(new LiteralControl("<b>Page Size: </b>"));
cell.Controls.Add(ddlpagesize);
tbl.Rows[0].Cells.AddAt(0, cell);
}
}
}
}
}
protected void ddlpagesize_SelectedIndexChanged(object sender, EventArgs e)
{
using (DropDownList ddlpagesize = (DropDownList)sender)
{
gv_transaction.PageSize = int.Parse(ddlpagesize.SelectedValue);
gv_transaction.PageIndex = 0;
BindTransactionGrid();
}
}
Now, SelectedIndex change event is not firing, when I change the dropdownlist value.
But interestingly, when I remove the using statement from the initiation of page size Dropdownlist; Selectedindex event is firing perfectly. Please tell me if there is any relation with the disposing of dropdownlist and selectedIndex Changed event for the dynamic dropdown in a Gridview
You don't need to wrap asp.net controls in using statements, asp.net will call dispose automatically on your controls, i think your using statements are causing them to be disposed too early.

Categories