now i am very confused with how to create gridview with the following structure and criteria:
note: the gridview will take data from different tables
columns:
1. IDcol the unique value and it will be from tableA(as text)
2. Date it will from also tableA(as text)
3. link1 it will be hyperlink to another page and the parameter for the url will be ‘IDcol’ column value but the text displayed will changed if this record is exists in tableB with the same ‘IDcol’ the dispalyed will be “view/edit” if not exists it will be “Add New”
database structure:
tableA:
IDcol as (primary key),
Date
tableB:
ID,
IDcol as (foreign key from tableA).
other fields
so i need populate the gridview using loop because i have to check for each row and use some conditions
sorry if my way of description is not clear but i am really confused
My code for deleting part:
<asp:LinkButton ID="DeleteLink" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
DeleteCommand="DELETE VisitsInfo WHERE ID=@VID">
<DeleteParameters>
<asp:Parameter Name="VID" Type="Int64" />
</DeleteParameters>
in code behind :
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int VID = int.Parse(GridView1.DataKeys[0].Value.ToString());
SqlDataSourceVisits.DeleteParameters[0].DefaultValue = VID.ToString();
}
when i click on delete link to delete row it works but when refresh the page its delete another row without click the delete link so why that happened??
No matter how many tables you are using to display data in a Gridview, the best and more flexible approach is to use OnRowDataBound.
And as Pranay said, you may wish to use Linq and join to get the required data from all tables, though, you can still get them in any other way you feel comfortable with