Does anyone know how to use the gridview delete functionality with Subsonic 3?
I am trying to delete rows that do not have the primary key displayed in the the gridview so I can't just pull that data from the gridview row. I was wondering if there is a way to do it with the DataKeyNames property.
Thanks..
Figured it out:
This is what you do:
<asp:GridView ID="PageGrid" runat="server" OnRowDeleting="DeleteTheRow" AutoGenerateDeleteButton="true"
AutoGenerateColumns="false" CssClass="centeredTableList" DataKeyNames="page_id">
<Columns>
<asp:BoundField DataField="page_name" HeaderText="Page Name" />
<asp:HyperLinkField Text="Edit" DataNavigateUrlFormatString="p={0}"
DataNavigateUrlFields="page_id" HeaderText="Edit"/>
</Columns>
</asp:GridView>
protected void DeleteTheRow(Object sender, GridViewDeleteEventArgs e)
{
int i = Convert.ToInt32(PageGrid.DataKeys[e.RowIndex].Value);
}
I'm not sure if this is related or even applicable to a web-based Grid, but I usually use a BindingSource that I fill with my 'subsonicized' items like
bindingsource.DataSource = new ItemColleciton.Load();
Then when you need what is currently selected, I would get the Item with, for instance,
int pk = (bindingSource.Current as Item).PrimaryKey;
That way I think you get the actualy selected record, since maybe the RowIndex ina grid, may or may not be the correct one depending on the sorting or filtering?
Hope this helps.
Related
I have aspxcombobox(Devexpress)
asp.net :
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<dx:ListBoxColumn FieldName="cg_id" />
<dx:ListBoxColumn FieldName="cg_name" />
</Columns>
</dx:ASPxComboBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TravelConnectionString %>" SelectCommand="SELECT * FROM [Categorys_Group]"></asp:SqlDataSource>
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Accounting/Check.aspx?id=" + ASPxComboBox1.SelectedItem.GetValue("cg_name"));
}
When click button. i want to get value index selected of aspxcombobox.
I try get value of combobox, but it only return value first ( = 0 ).
WHo can help me? get value of aspxcombobox.
This problem may be caused by wrong selectedItem or selectedIndex property or when aspxcombobox is empty etc.
MessageBox.show(ASPxComboBox1.Value != null? ASPxComboBox1.Value.ToString():string.Empty);
To skip from this error you should know ValueType property properly.
And also see it..
ASPxClientComboBox_GetSelectedItem
You have 2 choices:
on the code behind, create a switch case on the index selected, and bindes the data to comboBox accordingly.
on the data level: in your data base access layer, using your Data Provider class that fetches data from your database, you have to create a GET method that takes the SELECTED INDEX as a parameter, pass that SELECTED INDEX to a pre-defined STORED PROCEDURE and do a SELECT according to the choosen index.
I would suggest using the second solution for the following reasons:
no changes made on the code behind side.
fetch is done on the database level according to the selected index, which assures the correct data imported. --> Your Data Source is Bound earlier in the process.
small changes are made on the STORED PROCEDURE :)
Best Regards,
ANDOURA
I am creating a web application in ASP.net/VB.NET and I have an issue with the GridView control.
Currently, I have the GridView populated with data from the DB and I've also coded the update button to allow the user to edit any necessary information through a form that pops up.
What I'd like to do, if possible, is add a button to the two right columns(I already put one in the Dock Out Time column) which will be invisible if the column is set or will set the current time to the column. Setting the time for those two columns is already handled through the update form, but my supervisor asked me to try and see if this was possible.
Those two Time columns are TemplateFields(since I format the display time from what is actually in the DB) and I added an asp button in the ItemTemplate for that Set Button in the picture.
Is this even possible to do and if so, how would I access this button in the code behind so I can add functionality(setting the time and hiding it if the column is not null)If it's not really possible to have two items like this in a TemplateField I can just make 2 extra columns for these buttons but I think this would look much cleaner.
Any input would be greatly appreciated. thank you for your time.
Yes this is possible, check this answer:
https://stackoverflow.com/a/11077709/1268570
Basically you need to handle the RowCommand event from the grid and identify each button with a command, optionally you can add arguments to each button when you bind, for example:
<asp:GridView runat="server" OnRowCommand="grdProducts_RowCommand"
ID="grdProducts">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false"
CommandName="myLink" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' Text="Button"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
In code behind:
protected void grdProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "myLink":
this.lblMessage.Text = e.CommandName + " " + e.CommandArgument + " " + DateTime.Now.ToString();
// referenece to the current row
var row = this.grdProducts.Rows[int.Parse(e.CommandArgument.ToString())];
break;
default:
break;
}
}
After you update your grid in the RowCommand event, you should repopulate the grid data to render the changes
How can I get the selected item on the SelectedIndexChanging handler when using two SelectCommands? I can get the selected row through e.SelectedRow but I'm unable to get the selected column.
It's correct to have more than one SelectCommand in a GridView? If not, what's the best way?
You don't select a column in a gridview, you select a row. If you want a particular field of a row to be "selectable" you might consider using a HyperLinkField or a ButtonField and handle the events for that. But to my knowledge, admittedly it's limited, there is no way to be able to know, purely with a GridView and its SelectedRow Property which field in the row was "selected" when the row was selected.
You don't have to use select commands. you can use template fields and add a named command to it then you can check which of them was clicked in the RowCommand event (and u can also get the row index as well) see below.
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false"
CommandName="MyCommand" Text="Button" CommandArgument='<%# Container.DataItemIndex %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
RowCommend Event below
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("MyCommand"))
{
int row = Int32.Parse(e.CommandArgument.ToString());
}
}
hi I have this gridview like this.
<asp:DropDownList ID="triggerDropDown" runat="server" AutoPostBack="true" onselectedindexchanged="triggerDropDown_SelectedIndexChanged">
<asp:GridView ID="myGridView" run="server">
<Columns>
<asp:TemplateField HeaderText="Column 1">
<ItemTemplate>
<asp:DropDownList ID="myDropDown1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column 2">
<ItemTemplate>
<asp:DropDownList ID="myDropDown2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
now, when I change my triggerDropDown I want to change also all of the DropDowns inside Column 1 how can I do that?
protected void triggerDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
// what should I do here?
}
Inside your event method you should access the DropDownList that resides within each row of the GridView. Doing this you can bind each DropDownList to whatever data you want.
This link shows you how to do that:
http://www.velocityreviews.com/forums/t191319-need-help-with-accessing-a-control-within-a-template-field.html
Basically:
Iterate over each row of your GridView;
Find the DropDownList control with something like:
DropDownList mddl = GridView.Rows[2].FindControl("myDropDown1");
Bind new data to mddl.
The gridview is very likely not what you want here. The way to change the value of a control contained in a row is usually through grabbing a handle to the desired control using e.Item.FindControl() from within the ItemDataBound event of the gridview.
The problem with your approach is that you're wanting a control outside of the gridview (triggerDropDown) to interact with a single row of the gridview. Do you want the first row, first column, last row, first column or first column for each of the items in the grid? It's probably better you take the target of your trigger dropdown and place it outside of the gridview and deal with it directly.
If you really intend to change items in a row in the grid consider doing so in the ItemDataBound event of the gridview and you'll find lots of examples out there.
Actually I can use GridViewRow :) I just have to find the GridViewControl and get its Rows attribute which is a GridViewRow and now I can do a foreach of each row.
foreach (GridViewRow gridViewRow in (this.FindControl("myGridView") as GridView).Rows)
{
// I can see all elements of my row here as if I am traversing on GridViewEvents
}
I'm trying to add a lookup type column to a gridview. The column will be used as a picklist of values for the user to, well, pick a value from. Sounds fairly straight forward but the problem is I want the picklist column to be a distinct list from a table that is not in the datasource of the gridview. If I join the picklist table into my datasource I may not get the full list of values, if I create a union I suspect I'll get multiple entries in the picklist column. So I think I need to join the picklist column to another datasource or...? Dunno? over to you folks (hopefully) and thanks for any help offered.
WILL
You're going to need to make an edit template that has a dropdown list in it. Then inside your gridview's databound event, you'll need code to fill the ddl:
protected void grdGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (grdGrid.EditIndex == e.Row.RowIndex)
{
ddlMine = (DropDownList)e.Row.Cells[0].FindControl("ddlMine");
//bind ddlMine
}
}
You can create a template column that contains a dropdownlist. You can then have the dropdownlist be databound by anything including a seperate objectdatasource.
<asp:TemplateField HeaderText="Lookup">
<itemtemplate>
<asp:DropDownList runat="server" DataSourceId="SeperateDataSource" SelectedValue='<%# Bind("ValueThatIsSelectedUID") %>' ></asp:DropDownList>
<asp:ObjectDataSource runat="server" id="SeperateDataSource" selectmethod="SomeSelectMethod"></asp:ObjectDataSource>
</itemtemplate>
</asp:TemplateField>
The template field also supports edititemtemplate, footertemplate and headertemplate.