views:

48

answers:

3

I have a GridView in my page :

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BackColor="White"
    AutoGenerateColumns="False" EmptyDataText="No data available." BorderColor="#DEDFDE"
    BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="729px" ForeColor="Black"
    GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <RowStyle BackColor="#F7F7DE" />
    <Columns>
        <asp:TemplateField HeaderText="TransactionKey" SortExpression="TransactionKey">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewTransactionKey" runat="server" Text='<%# Bind("TextBoxTransactionKey") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewTransactionKey" runat="server" Text='<%# Bind("TextBoxTransactionKey") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="TerminalID" SortExpression="TerminalID">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewTerminalID" runat="server" Text='<%# Bind("TextBoxTerminalID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewTerminalID" runat="server" Text='<%# Bind("TextBoxTerminalID") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="MerchantID" SortExpression="MerchantID">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewMerchantID" runat="server" Text='<%# Bind("TextBoxMerchantID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewMerchantID" runat="server" Text='<%# Bind("TextBoxMerchantID") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="شماره حساب" SortExpression="شماره حساب">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewBankAccount" runat="server" Text='<%# Bind("TextBoxBankAccount") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewBankAccount" runat="server" Text='<%# Bind("TextBoxBankAccount") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="نام بانک" SortExpression="نام بانک">
            <EditItemTemplate>
                <asp:TextBox ID="TextBoxGridViewBankName" runat="server" Text='<%# Bind("BankName") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelGridViewBankName" runat="server" Text='<%# Bind("BankName") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Size="14px" />
            <ItemStyle Font-Size="12px" />
        </asp:TemplateField>
        <asp:CommandField ShowSelectButton="True" >
        <ItemStyle Font-Size="12px" />
        </asp:CommandField>
    </Columns>
    <FooterStyle BackColor="#CCCC99" />
    <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>

I set the DataSource with the follwing code :

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        LabelTitr.Text = "Add Banks";
        LabelResult.Text = "";
        if (Session["Username"] != null)
        {
            string permission = "";
            bool login = PublicMethods.CheckUsernamePass(Session["Username"].ToString(), Session["Password"].ToString(), out permission);
            if (!login)
            {
                permission = "";
                Response.Redirect("../Default.aspx");
                Session["Username"] = Session["Password"] = null;
                return;
            }
        }
        else
        {
            Response.Redirect("../Default.aspx");
            Session["Username"] = Session["Password"] = null;
            return;
        }

        if (!IsPostBack)
            BindDataToGridView1();
    }
    catch (Exception ex)
    {
        LabelResult.Text = ex.Message;
    }
}

void BindDataToGridView1()
{
    try
    {
        DataSet1 dataSet = new DataSet1();
        DataClasses2DataContext dbc2 = new DataClasses2DataContext();
        var Definitions = dbc2.Definitions;
        if (Definitions.Count() <= 0) return;
        foreach (var Definition in Definitions)
        {
            string bankName = dbc2.Banks.Where(c => c.BankID == Definition.BankID).First().BankName;
            string CheckBox = "<input name=\"CheckBoxSubmitChanges\" type=\"checkbox\" value=" + Definition.DefinitionID + " />";
            dataSet.DataTableBanks.Rows.Add(bankName, Definition.BankAccount, Definition.MerchantID, Definition.TerminalID, Definition.TerminalID);
            GridView1.DataSource = dataSet.DataTableBanks;
        }
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        LabelResult.Text = ex.Message;
    }
}

and this is GridView1_SelectedIndexChanged method :

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    string temp = row.Cells[4].Text;
    LabelResult.Text = temp;
}

But always temp string is empty !!!
What's wrong with it ?
Thanks

+1  A: 

Hey,

If it's a TemplateField, you have to use the FindControl("control ID") option, not the text fo the cell. I count cell 4 as being a templatefield...

Brian
+1  A: 

There is no Text as your data is stored in controls. You need to search for the control you want and pull the Text out of.

Eg:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    GridViewRow row = GridView1.SelectedRow; 
    string temp = row.Cells[4].FindControl('LabelGridViewBankName').Text; 
    LabelResult.Text = temp; 
}

Optionally you could could key off the DataKey property if you are using it and then use it to search your datasource for the value you want.

Kelsey
+1  A: 
//this will give you control over the textbox object
var field = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("your_control_ID"));
//and here you can access the text
string temp = field.Text;
Eton B.