Add Controls to ListView

  • Thread starter Thread starter Alan
  • Start date Start date
A

Alan

Hi All,

Is it possible to add other controls to the ListView control in C# windows
forms. i.e. I want to add a button column so to speak to the list view. You
can do this sort of thing with the datagrid in asp.net, however I think the
datagrid in c# is overly complex for what I'm wanting to do, and I'm not
even sure it is possible to do it in that.

Answers to both Controls would be appreciated.

Thanks

Alan
 
Hi Alan
Yes you can add a button column to datagrid control. You can then write
event to handle clicks on that control ( in the event handler of…….. you
can handle event according to which button column was clicked… and on
which row if the datagrid it was clicked … and her is an example
First , you will add the datagrid control to the aspx page as follows


<asp:datagrid DataKeyField="DiscId" id="DataGrid1" style="Z-INDEX: 110;
LEFT: 8px; POSITION: absolute; TOP: 408px"
runat="server" AutoGenerateColumns="False" CellPadding="8"
BorderWidth="2px" BorderColor="Yellow">
<HeaderStyle ForeColor="White" BackColor="Indigo"></HeaderStyle>
<Columns>

<asp:BoundColumn DataField="FloatDiscNumber" SortExpression="DES"
HeaderText="FloatNumber">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="PackDescription" HeaderText="Catagory">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>

<asp:ButtonColumn Text="Book" ButtonType="PushButton"
HeaderText="Reserve" CommandName="reserve">
<ItemStyle BackColor="Purple"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="DVD View">
<ItemStyle BackColor="Purple"></ItemStyle>
<ItemTemplate >
<asp:Button runat="server" Text="Details" CommandName="opencontent"
CausesValidation="false"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>



Then inside your code you define a handler to the datagrid ItemCommand
event as follows

private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName.Equals("reserve"))// here you know
// which button column was clicked
{
disks.row.PrimaryKey = new DataColumn[]{disks.row.Columns["DiscId"]};
DataRow row =
disks.row.Rows.Find(DataGrid1.DataKeys[e.Item.ItemIndex]);which column
//was clicked
//do some stuff here
}
else
{
disks.row.PrimaryKey = new DataColumn[]{disks.row.Columns["DiscId"]};
DataRow row = disks.row.Rows.Find(DataGrid1.DataKeys[e.Item.ItemIndex]);
//do some stuff here
}
}


Hope this would help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top