DataKey Field problem

Discussion in 'ASP .Net Datagrid Control' started by Phani, Apr 26, 2005.

  1. Phani

    Phani Guest

    Hi everyone !!!

    I have a problem here with the DataKey
    when i try to retrive the data from the DataKeys[e.Item.ItemIndex] i get
    nothing out of it ...

    can anyone tell me the reason for this ....plz do help me

    The main thing i want to do is
    display a datagrid first and then depending on the datakey value get the
    data for the second datagrid from the database ..... using DAL ....

    Here is the code for my thing ....
    Plz help me out


    <TR>
    <TD><asp:datagrid id="DataGrid1" runat="server" UseAccessibleHeader="True"
    AllowSorting="True" Font-Size="Smaller"
    Font-Names="Tahoma" AutoGenerateColumns="False" Width="100%"
    DataSource="<%# dataSet11 %>" DataKeyField="ProjectId"
    DataMember="Projects">
    <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
    <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White"
    VerticalAlign="Middle"
    BackColor="#6699CC"></HeaderStyle>
    <Columns>
    <asp:ButtonColumn Text="+" ButtonType="PushButton"
    CommandName="SelectProject"></asp:ButtonColumn>
    <asp:BoundColumn DataField="ProjectId" SortExpression="ProjectId"
    HeaderText="Project Id"></asp:BoundColumn>
    <asp:BoundColumn DataField="ProjectName" SortExpression="ProjectName"
    HeaderText="Project Name"></asp:BoundColumn>
    <asp:BoundColumn DataField="ProjectDescription"
    SortExpression="ProjectDescription" HeaderText="Project
    Description"></asp:BoundColumn>
    <asp:BoundColumn DataField="ProjectStartDate"
    SortExpression="ProjectStartDate" HeaderText="Start Date"></asp:BoundColumn>
    </Columns>
    </asp:datagrid></TD>
    </TR>
    <TR>
    <TD><asp:datagrid id="DataGrid2" runat="server" UseAccessibleHeader="True"
    AllowSorting="True" Font-Size="Smaller"
    Font-Names="Tahoma" Width="100%">
    <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
    <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White"
    VerticalAlign="Middle"
    BackColor="#6699CC"></HeaderStyle>
    </asp:datagrid></TD>
    </TR>


    and this is the code behind

    private void Page_Load(object sender, System.EventArgs e)
    {
    sqlDataAdapter1.Fill (dataSet11,"Projects");
    sqlDataAdapter2.Fill (dataSet11,"Tasks");

    DataColumn dc = dataSet11.Tables ["Projects"].Columns ["ProjectId"];
    DataColumn dc1 = dataSet11.Tables ["Tasks"].Columns ["ProjectId"];
    DataRelation dr = new DataRelation ("ProjectsTasks", dc,dc1, false);
    dataSet11.Relations .Add (dr);

    DataGrid1.DataSource =dataSet11;
    DataGrid1.DataMember ="Projects";
    DataGrid1.DataBind ();
    }

    private void Item_click(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    int key = Convert.ToInt32 (DataGrid1.DataKeys[e.Item.ItemIndex]);

    Button1.Text =DataGrid1.DataKeys [e.Item .ItemIndex ].ToString ();
    TasksDB task=new TasksDB ();
    DataSet ds = task.GetTasks (key);

    DataGrid2.DataSource =ds;
    DataGrid2.DataBind ();
    }
    Phani, Apr 26, 2005
    #1
    1. Advertising

  2. Phani

    Phani Guest

    Hi again ....

    I figured out the problem
    there is a bug in ASP.Net ..... the OnItemCommand does not work for push
    button .....
    i changed the button type to link button and it is working fine ....

    anyway thanks
    Phani
    Phani, Apr 26, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Ormaetxe

    Datakey in Datagrid

    J. Ormaetxe, Oct 28, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    6,635
    Egbert Nierop \(MVP for IIS\)
    Oct 29, 2003
  2. Jim in Arizona
    Replies:
    1
    Views:
    3,208
    Jim in Arizona
    Sep 18, 2006
  3. Anil TG

    Problem With DataList Datakey

    Anil TG, Aug 19, 2009, in forum: ASP .Net
    Replies:
    2
    Views:
    708
    Anil TG
    Aug 20, 2009
  4. Sound
    Replies:
    2
    Views:
    428
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    405
Loading...

Share This Page