Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create Grid view with customized column from different table with asp.net and c#?

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??

like image 530
special life Avatar asked Jan 21 '26 23:01

special life


1 Answers

No matter how many tables you are using to display data in a Gridview, the best and more 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 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

like image 122
m.othman Avatar answered Jan 23 '26 12:01

m.othman