How to create a custom grid view from another table with asp.net and c #?

I am now very confused about how to create a gridview with the following structure and criteria:

note: gridview will accept data from different tables

columns: 1. IDcol is a unique value, and it will be from tableA (as text) 2. Date when it will also be with tableA (as text) 3. link1 this will be a hyperlink to another page, and the parameter for url will be the value "IDcol" but the displayed text will be changed if this record exists in table B with the same "IDcol" that the dispatched one will be "view / edit" "if it does not exist, 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 to populate the gridview using a loop, because I have to check every row and use some conditions

sorry if my description method is not clear but i really got confused


My code to remove the 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 the 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 the delete link to delete the line that works, but when the page is refreshed, the other line is deleted without the delete link, so why did this happen?

+4
source share
3 answers

No matter how many tables you use to display data in a Gridview, the best and most flexible approach is to use OnRowDataBound.

 //do some database queries to return the appropriate value DataTable dt = new DataTable(); private void Bind() { //set up the dt with all the required data from single or multiple tables } //Then in the page Load method, call the above Bind method protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); } } //then do something like the following int idx = 0; protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //do some database queries to return the appropriate value, then do something like the following if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[0].Text = dt.Rows[idx][0]; e.Row.Cells[1].Text = dt.Rows[idx][1]; //or maybe ((TextBox)e.Row.Cells[0].FindControl("textBox1")).Text = dt.Rows[idx][0]; ((Label)e.Row.Cells[1].FindControl("label2")).Text = dt.Rows[idx][1]; ((CheckBox)e.Row.Cells[1].FindControl("chkbx1")).Selected = (bool)dt.Rows[idx][2]; idx++; } } 

And as Pranayai said, you can use Linq and join to get the required data from all tables, although you can still get it in any other way, you will be comfortable with

+2
source

Create a dataview from two tables and snap a grid to it. When rendering a GridView in row events, i.e. rowbound, you can put your conditions.

0
source

If I get it right, you want to select some column (e.g. A, B, C) from several tables and then show them in a gridview. If so, this can be done in many ways. 1. Create a custom DataSet with the necessary data in it and fill it with the selection request. Then bind the dataset to the gridview. 2. Create a DataTable with the necessary data in it and populate it with a select query. Then bind the table to the gridview. 3. Add the necessary columns directly to the gridview, then add data elements to it using a loop. Note: In either case, the UNCHECK AutoGenerateColumn "gridview parameter is for smooth operation.

0
source

Source: https://habr.com/ru/post/1401305/


All Articles